Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Wrap text within grid.table when string exceeds set length

Tags:

r

gridextra

I am using grid.table within the gridExtra package to display a list of survey comments in table format. When the comments(string variable) exceeds a given length I want it to automatically insert a line break "\n".

library(gridExtra)
df<-data.frame(comments = c("Here is a short string", 
"Here is a long string that needs to be broken in half so that it doesn't run off the page",
"Here is another short string"))

grid.newpage()
print(grid.table(df$comments))

I am open to using a different table package if this feature is available elsewhere.

like image 297
Braden Avatar asked Sep 10 '14 13:09

Braden


1 Answers

you can use strwrap,

 d = sapply(lapply(df$comments, strwrap, width=50), paste, collapse="\n")
 grid.table(d)
like image 141
baptiste Avatar answered Oct 26 '22 07:10

baptiste