6

我在 gridExtra 包中使用 grid.table 以表格格式显示调查评论列表。当注释(字符串变量)超过给定长度时,我希望它自动插入换行符“\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))

如果此功能在其他地方可用,我愿意使用不同的表包。

4

1 回答 1

5

你可以使用strwrap,

 d = sapply(lapply(df$comments, strwrap, width=50), paste, collapse="\n")
 grid.table(d)
于 2014-09-10T14:50:33.090 回答