Search code examples
rr-markdownrenderingflextable

Using Flextable to Word for Free-Text Survey Responses


I am doing some survey analysis and am stumped with a weird issue.

When I try to write out a flextable object to Word, I'm getting a blank document of ~30 pages. This has previously worked in R and R Markdown without any issues, and I can't figure out why it's happening.

Here is the dput:

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"
))

Here's the code I'm using to write to a directory that I am 100% sure I have access to:

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")

Any ideas to help resolve or troubleshoot this? Thanks in advance for any assistance!


Solution

  • As mentioned by David Gohel, adding a width argument solved the issue:

    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")