# 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")
state. <- discrete_format(
"Germany" = 1:16,
"Schleswig-Holstein" = 1,
"Hamburg" = 2,
"Lower Saxony" = 3,
"Bremen" = 4,
"North Rhine-Westphalia" = 5,
"Hesse" = 6,
"Rhineland-Palatinate" = 7,
"Baden-Württemberg" = 8,
"Bavaria" = 9,
"Saarland" = 10,
"West" = 1:10,
"Berlin" = 11,
"Brandenburg" = 12,
"Mecklenburg-Western Pomerania" = 13,
"Saxony" = 14,
"Saxony-Anhalt" = 15,
"Thuringia" = 16,
"East" = 11:16)
# Define style
set_style_options(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.
set_titles("This is title number 1 link: https://cran.r-project.org/",
"This is title number 2",
"This is title number 3")
set_footnotes("This is footnote number 1",
"This is footnote number 2",
"This is footnote number 3 link: https://cran.r-project.org/")
# Output complex tables with different percentages
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.),
na.rm = TRUE)
# If you want to get a clearer vision of what the result table looks like, in terms
# of the row and column categories, you can write the code like this, to make out
# the variable crossings and see the order.
my_data |> any_table(columns = c( "year", "education + year"),
rows = c("sex + age",
"sex",
"age"),
values = weight,
statistics = c("sum", "pct_group"),
pct_group = c("sex", "age", "education", "year"),
formats = list(sex = sex., age = age.,
education = education.),
na.rm = TRUE)
# Percentages based on value variables instead of categories
my_data |> any_table(rows = c("age + year"),
columns = "sex",
values = c(probability, person),
statistics = c("pct_value", "sum", "freq"),
pct_value = list(rate = "probability / person"),
weight = weight,
formats = list(sex = sex., age = age.),
na.rm = TRUE)
# Customize the visual appearance by adding variable and statistic labels. Both
# can also be set as a global option, if labels should be reused over multiple
# tables.
# Note: You don't have to describe every element. Sometimes a table can be more
# readable with less text. To completely remove a variable label just put in an
# empty text "" as label.
my_data |> any_table(rows = c("age + year"),
columns = "sex",
values = weight,
statistics = c("sum", "pct_group"),
order_by = "interleaved",
formats = list(sex = sex., age = age.),
var_labels = list(age = "Age categories",
sex = "", weight = ""),
stat_labels = list(pct = "%"),
na.rm = TRUE)
# Individual styling can also be passed directly
my_style <- excel_output_style(header_back_color = "0077B6",
font = "Times New Roman")
my_data |> any_table(rows = c("age + year"),
columns = "sex",
values = c(probability, person),
statistics = c("pct_value", "sum", "freq"),
pct_value = list(rate = "probability / person"),
weight = weight,
formats = list(sex = sex., age = age.),
style = my_style,
na.rm = TRUE)
# Pass on workbook to create more sheets in the same file
my_style <- my_style |> modify_output_style(sheet_name = "age_sex")
result_list <- my_data |>
any_table(rows = "age",
columns = "sex",
values = weight,
statistics = "sum",
formats = list(sex = sex., age = age.),
style = my_style,
na.rm = TRUE,
print = FALSE)
my_style <- my_style |> modify_output_style(sheet_name = "edu_year")
my_data |> any_table(workbook = result_list[["workbook"]],
rows = "education",
columns = "year",
values = weight,
statistics = "pct_group",
formats = list(education = education.),
style = my_style,
na.rm = TRUE)
# The result list from above also carries the transformed data frame if
# needed for further usage
any_table_df <- result_list[["table"]]
# Output multiple complex tables by expressions of another variable.
# If you specify the sheet name as "by" in the output style, the sheet
# names are named by the variable expressions of the by-variable. Otherwise
# the given sheet named gets a running number.
my_style <- my_style |> modify_output_style(sheet_name = "by")
my_data |> any_table(rows = c("sex", "age"),
columns = "education + year",
values = weight,
by = state,
statistics = c("sum", "pct_group"),
pct_group = "education",
formats = list(sex = sex., age = age., state = state.,
education = education.),
na.rm = TRUE)
# To save a table as xlsx file you have to set the path and filename in the
# style element
# Example files paths
table_file <- tempfile(fileext = ".xlsx")
# Note: Normally you would directly input the path ("C:/MyPath/") and name ("MyFile.xlsx").
set_style_options(save_path = dirname(table_file),
file = basename(table_file),
sheet_name = "MyTable")
my_data |> any_table(rows = "sex",
columns = "year",
values = weight,
formats = list(sex = sex.))
# Manual cleanup for example
unlink(table_file)
# Global options are permanently active until the current R session is closed.
# There are also functions to reset the values manually.
reset_style_options()
reset_qol_options()
close_file()
Run the code above in your browser using DataLab