打印到控制台时如何在 R 中的列中换行文本?

How to wrap text within a column in R when printing to the console?

我正在尝试调整 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)

这是我所在的位置:

看看 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 
#>---------------------------