# 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/")
# 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.),
style = my_style,
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.),
style = my_style,
na.rm = TRUE)
# Percentages based on value variables instead of categories
my_data |> any_table(rows = c("age + year"),
columns = c("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)
# Customize the visual appearance by adding titles, footnotes and variable
# and statistic labels.
# 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 = c("sex"),
values = weight,
statistics = c("sum", "pct_group"),
order_by = "interleaved",
formats = list(sex = sex., age = age.),
titles = titles,
footnotes = footnotes,
var_labels = list(age = "Age categories",
sex = "", weight = ""),
stat_labels = list(pct = "%"),
style = my_style,
na.rm = TRUE)
# With individual styling
my_style <- my_style |> modify_output_style(header_back_color = "0077B6",
font = "Times New Roman")
my_data |> any_table(rows = c("age + year"),
columns = c("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 = c("age"),
columns = c("sex"),
values = weight,
statistics = c("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 = c("education"),
columns = c("year"),
values = weight,
statistics = c("pct_group"),
formats = list(education = education.),
style = my_style,
na.rm = TRUE)
# 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 = c("education + year"),
values = weight,
by = state,
statistics = c("sum", "pct_group"),
pct_group = c("education"),
formats = list(sex = sex., age = age., state = state.,
education = education.),
titles = titles,
footnotes = footnotes,
style = my_style,
na.rm = TRUE)
Run the code above in your browser using DataLab