Skip to contents

Combines any number of tables created with any_table() into one workbook and styles them according to their meta information.

Usage

combine_into_workbook(
  ...,
  file = NULL,
  output = "excel",
  print = TRUE,
  monitor = FALSE
)

Arguments

...

Provide any number of result lists output by any_table().

file

If NULL, opens the output as temporary file. If a filename with path is specified, saves the output to the specified path.

output

The following output formats are available: excel and excel_nostyle.

print

TRUE by default. If TRUE prints the output, if FALSE doesn't print anything. Can be used if one only wants to catch the combined workbook.

monitor

FALSE by default. If TRUE outputs two charts to visualize the functions time consumption.

Value

A fully styled workbook containing the provided tables.

Examples

# Example data frame
my_data <- dummy_data(1000)
my_data[["person"]] <- 1

# Formats
age. <- discrete_format(
    "Total"          = 0:100,
    "under 18"       = 0:17,
    "18 to under 25" = 18:24,
    "25 to under 55" = 25:54,
    "55 to under 65" = 55:64,
    "65 and older"   = 65:100)

sex. <- discrete_format(
    "Total"  = 1:2,
    "Male"   = 1,
    "Female" = 2)

education. <- discrete_format(
    "Total"            = c("low", "middle", "high"),
    "low education"    = "low",
    "middle education" = "middle",
    "high education"   = "high")

# Define style
my_style <- excel_output_style(column_widths = c(2, 15, 15, 15, 9))

# Define titles and footnotes. If you want to add hyperlinks you can do so by
# adding "link:" followed by the hyperlink to the main text.
titles <- c("This is title number 1 link: https://cran.r-project.org/",
            "This is title number 2",
            "This is title number 3")
footnotes <- c("This is footnote number 1",
               "This is footnote number 2",
               "This is footnote number 3 link: https://cran.r-project.org/")

# Catch the output and additionally use the options:
# pint = FALSE and output = "excel_nostyle".
# This skips the styling and output part, so that the function runs faster.
# The styling is done later on.
my_style <- my_style |> modify_output_style(sheet_name = "big table")

tab1 <- my_data |> any_table(rows       = c("sex + age", "sex", "age"),
                             columns    = c("year", "education + year"),
                             values     = weight,
                             statistics = c("sum", "pct_group"),
                             pct_group  = c("sex", "age", "education", "year"),
                             formats    = list(sex = sex., age = age.,
                                               education = education.),
                             style      = my_style,
                             na.rm      = TRUE,
                             print      = FALSE,
                             output     = "excel_nostyle")

my_style <- my_style |> modify_output_style(sheet_name = "age_sex")

tab2 <- my_data |> any_table(rows       = c("age"),
                             columns    = c("sex"),
                             values     = weight,
                             statistics = c("sum"),
                             formats    = list(sex = sex., age = age.),
                             style      = my_style,
                             na.rm      = TRUE,
                             print      = FALSE,
                             output     = "excel_nostyle")

my_style <- my_style |> modify_output_style(sheet_name = "edu_year")

tab3 <- my_data |> any_table(rows       = c("education"),
                             columns    = c("year"),
                             values     = weight,
                             statistics = c("pct_group"),
                             formats    = list(education = education.),
                             style      = my_style,
                             na.rm      = TRUE,
                             print      = FALSE,
                             output     = "excel_nostyle")

# Every of the above tabs is a list, which contains the data table, an unstyled
# workbook and the meta information needed for the individual styling. These
# tabs can be input into the following function, which reads the meta information,
# styles each table individually and combines them as separate sheets into a single workbook.
combine_into_workbook(tab1, tab2, tab3)