Skip to contents

Add a list of crosstables in an officer document

Usage

body_add_crosstable_list(doc, l, fun = "title2", ...)

body_add_flextable_list(doc, l, fun = "title2", ...)

Arguments

doc

a rdocx object, created by officer::read_docx()

l

a named list of tables. Plain dataframes will be converted to flextables.

fun

a function to be used before each table, most likely to add some kind of title. Should be of the form function(doc, .name) where .name is the name of the current crosstable of the list. You can also pass "title2" to add the name as a title of level 2 between each table, "newline" to simply add a new line, or even NULL to not separate them (beware that the table might merge then).

...

arguments passed on to body_add_crosstable() or body_add_flextable()

Value

The docx object doc

Examples

library(officer)
ctl = list(iris2=crosstable(iris2, 1),
           mtcars2=crosstable(mtcars2, 1),
           "just a flextable"=flextable::flextable(mtcars2[1:5,1:5]))

myfun = function(doc, .name){
    doc %>%
        body_add_title(" This is table '{.name}' as a flex/crosstable", level=2) %>%
        body_add_normal("Here is the table:")
}

read_docx() %>%
    body_add_title("Separated by subtitle", 1) %>%
    body_add_crosstable_list(ctl, fun="title2") %>%
    body_add_title("Separated by new line", 1) %>%
    body_add_crosstable_list(ctl, fun="newline") %>%
    body_add_title("Separated using a custom function", 1) %>%
    body_add_crosstable_list(ctl, fun=myfun, body_fontsize=8) %>%
    write_and_open()