I create MS Word documents using Rmarkdown and officer/officedown packages. My output is officdown::rdocx_document.
I have code like this in my Rmarkdown file:
library(flextable)
library(officer)
tables <- list(
flextable(iris),
flextable(mtcars)
# and potentially more tables, the list is dynamic
)
I want to toggle portrait/landscape layout depending on the number of columns of each table. For example, if the number of columns is greater than 5 the page orientation should be landscape, otherwise portrait.
I know that officer package provides block_section function, but I don't know how to use it dynamically. Please notice that I don't know how many tables will be there, the list is dynamic.
I tried this, according to the help page of block_section function:
orients <- calculate_page_orient(tables) # a helper that will determine the orientation based on the number of columns in a table
for (i in seq_len(lenght(tables))) {
block_section(prop_section(type = "continuous")
tables[[i]]
block_section(prop_section(page_size = page_size(orient = orients[i])))
}
But it doesn't work. How I can dynamically set the page layout using officer package?
Here is one possible implementation which instead of using
block_sectionuses!---BLOCK_LANDSCAPE_START/END--->to set the page layout to landscape and which uses chunk optionresults='asis'to dynamically render your tables within aforloop. Also note that I loop directly over the datasets and do the conversion to aflextableinside the loop.