2017-05-24 5 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 
#>--------------------------- 
相關問題