【问题标题】:How to wrap text within a column in R when printing to the console?打印到控制台时如何在 R 的列中换行?
【发布时间】:2017-05-24 15:22:32
【问题描述】:

我正在尝试调整 R 将数据框打印到控制台的方式。具体来说,我想打印一个数据框,使得列的文本在达到一定宽度后换行。理想情况下,我想要如下所示的内容:

     v1              v2     v3
1  TRUE Some text         TRUE
2  TRUE Some more text   FALSE
3  TRUE This text wraps  FALSE
        after a certain  
        width   
4 FALSE Even more text   FALSE
5  TRUE More text         TRUE

这是一个 MWE:

data.frame(v1 = c(TRUE, TRUE, TRUE, FALSE, TRUE), v2 = c("Some text", "Some more text", "This text wraps after a certain width", "Even more text", "More text"), y = c(TRUE, FALSE, FALSE, FALSE, TRUE))
options(width=10)

这是我所在的位置:

【问题讨论】:

    标签: r printing console word-wrap pander


    【解决方案1】:

    看看pander 库中的pandoc.table 函数。 http://rapporter.github.io/pander/pandoc_table.html 之后似乎在做你想做的事

    library(pander)
    m<-data.frame(v1 = c(TRUE, TRUE, TRUE, FALSE, TRUE), v2 = c("Some text", "Some more text", "This text wraps after a certain width", "Even more text", "More text"), y = c(TRUE, FALSE, FALSE, FALSE, TRUE))
    pandoc.table(m, split.cells = c(5, 20, 5))
    
    #>---------------------------
    #> v1         v2          y  
    #>----- --------------- -----
    #>TRUE     Some text    TRUE 
    #>
    #>TRUE  Some more text  FALSE
    #>
    #>TRUE  This text wraps FALSE
    #>      after a certain      
    #>           width           
    #>
    #>FALSE Even more text  FALSE
    #>
    #>TRUE     More text    TRUE 
    #>---------------------------
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-04
      • 2014-08-28
      • 1970-01-01
      • 2012-09-21
      • 2021-09-22
      • 2012-04-16
      相关资源
      最近更新 更多