简体   繁体   中英

Set width for flextable in a Word document with officer

I have a very simple task. Create a flextable (or qflextable) from a data frame. And then print it in Word. It is a 4 column table. The first column is a descriptive label and the other 3 are percentages or counts. In the first row below the header, first column, the cell contents are long but not excessive (about 10 words, so it should wrap 2-3 times).

When I print as qflextable (which is just flextable with auto fit, from what I gather), I get a table that doesn't fit on the page because it fits that first row without wrapping. When I used flextable, no matter what I do, it is a narrow table (each column is about 1") that fits and wraps the long text, but I can't seem to adjust its width from R.

Either way, if I open the Word document, and set the table properties to 6.5 inches, which is what I would like to do, the table looks great.

I can't tell if this is a flextable or officer issue. And I can't seem to figure out how to use table_width() or prop_table(table_width(6.5, "in")) in conjunction with a table. The help file doesn't have a parameter for a table -- the function inputs are simply width and unit.

My code is below:

at <- flextable(at)

tabdoc <- read_docx(path = "path here"))
tabdoc <- 
    body_add_par(tabdoc, value = sectcap, style = "heading 1") %>% 
    body_add_caption(table1cap) %>% 
    body_add_flextable(at) 

Any help with the syntax for setting the table width, or even setting individual column widths, would be appreciated.

A reproducible example would be useful.

What worked for me was to specifically set the width of the first column:

at <- width(qflextable(at), j=1, width=4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM