首页 > 解决方案 > 使用 Flextable to Word 进行自由文本调查回复

问题描述

我正在做一些调查分析,但遇到了一个奇怪的问题。

当我尝试将一个flextable对象写到 Word 中时,我得到一个大约 30 页的空白文档。这以前在 R 和 R Markdown 中有效,没有任何问题,我不知道为什么会这样。

这是输出:

df <- structure(list(Response = c("Comment from patron 1", "Comment from customer 2", 
"Comment from someone 3", "Comment from nobody 4", "Comment from patron 5", 
"Comment from customer 6", "Comment from someone 7", "Comment from nobody 8"
)), row.names = c(NA, -8L), class = c("tbl_df", "tbl", "data.frame"
))

这是我用来写入我 100% 确定可以访问的目录的代码:

ft <- as.data.frame(df)
ft <- flextable(ft)
ft <- theme_zebra(ft)
ft <- autofit(ft)

doc <- read_docx()
doc <- body_add_flextable(doc, value = ft)
print(doc, target = "Doc2.docx")

有什么想法可以帮助解决或解决此问题吗?提前感谢您的任何帮助!

标签: rr-markdownrenderingflextable

解决方案


正如 David Gohel 所提到的,添加一个宽度参数解决了这个问题:

ft <- as.data.frame(df)
ft <- flextable(ft)
ft <- theme_zebra(ft)
ft <- autofit(ft)
**ft <- width(ft, width = 7)**

doc <- read_docx()
doc <- body_add_flextable(doc, value = ft)
print(doc, target = "Doc2.docx")

推荐阅读