# Example data frame
my_data <- dummy_data(1000)
# 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 cross tables
my_data |> crosstabs(age, sex)
my_data |> crosstabs(age, sex,
weight = "weight")
# Also works with characters
my_data |> crosstabs("age", "sex")
my_data |> crosstabs("age", "sex",
weight = "weight")
# Applying 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)
my_data |> crosstabs(age, sex,
formats = list(age = age., sex = sex.))
# Split cross table by expressions of another variable
my_data |> crosstabs(age, sex, by = education)
# Compute different stats
my_data |> crosstabs(age, sex,
statistics = c("sum", "freq", "pct_row", "pct_column", "pct_total"))
# Get a list with two data tables for further usage
result_list <- my_data |> crosstabs(age, sex,
formats = list(age = age., sex = sex.))
# Output in text file
my_data |> crosstabs(age, sex, output = "text")
# Output to Excel
my_data |> crosstabs(age, sex, output = "excel")
# Individual styling can also be passed directly
my_style <- excel_output_style(header_back_color = "0077B6",
font = "Times New Roman")
my_data |> crosstabs(age, sex, output = "excel", style = my_style)
# 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 |> crosstabs(age, sex, output = "excel")
# 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