2017-05-24 75 views
1

我试图调整R打印数据帧到控制台的方式。具体来说,我想打印一个数据框,以便在达到一定宽度后,列的文本将进行换行。理想情况下,我想要的东西,看起来像下面这样:如何在打印到控制台时在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) 

这里就是我在:

enter image description here

回答

2

看看的pandoc.table函数在pander库中。它似乎在做什么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 
#>--------------------------- 
相关问题