| Title: | Quickly export survey results into MS Excel spreadsheet |
|---|---|
| Description: | Export data (usually) from opinion survey into a nicely readable MS Excel spreadsheet. Based on data.table and openxlsx2. Supports categorical variables only for now. |
| Authors: | Aleš Vomáčka [aut, cre] |
| Maintainer: | Aleš Vomáčka <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.3.0 |
| Built: | 2026-11-02 14:31:55 UTC |
| Source: | https://github.com/alesvomacka/spreadview |
Creates an Excel workbook containing frequency tables for multiple variables, optionally grouped and weighted. Each variable's table is row-bound together into a single sheet. When grouping is specified, cells are colored based on adjusted standardized residuals.
compose_spreadsheet( data, vars, group = NULL, weight = NULL, file = NULL, na.rm = TRUE, percent = TRUE )compose_spreadsheet( data, vars, group = NULL, weight = NULL, file = NULL, na.rm = TRUE, percent = TRUE )
data |
A data frame or data.table containing the variables to tabulate. |
vars |
Character vector of variable names to create tables for. Each
variable will be passed to |
group |
Character vector specifying grouping variables. Default is
|
weight |
Character string naming the weight variable to use for
weighted frequencies. Passed to |
file |
Character string specifying the output file path. If |
na.rm |
Should missing values be removed from the table? Defaults to TRUE. |
percent |
Logical. If |
The function iterates over each variable in vars, applies
compose_table() with the specified parameters, and combines the
results using data.table::rbindlist(). The resulting spreadsheet
is added to a new workbook with openxlsx2.
The header spans two rows:
Row 1: Column names for non-group columns (e.g., "label", "n", "total") and group variable names for grouped columns (e.g., "IDE_8")
Row 2: Empty strings for non-group columns, category names for grouped columns (e.g., "muž", "žena")
When grouping variables are specified, cells are colored based on adjusted standardized residuals:
Blue shades for positive residuals (more than expected)
Red shades for negative residuals (less than expected)
Color intensity increases with absolute residual value
Residuals beyond ±2 indicate significance at ~0.05 level
An openxlsx2 workbook object containing a single worksheet
with all frequency tables row-bound together. Cells are colored based on
adjusted standardized residuals when grouping is used.
compose_table, compute_adj_residuals
## Not run: compose_spreadsheet(data = penguins, var = c("species", "sex"), group = "island") ## End(Not run)## Not run: compose_spreadsheet(data = penguins, var = c("species", "sex"), group = "island") ## End(Not run)
Creates a frequency table for a categorical variable, optionally cross-tabulated by one or more grouping variables. The function automatically adds variable labels and standardizes column names for easier downstream processing.
compose_table( data, var, group = NULL, weight = NULL, prop = "none", na.rm = TRUE )compose_table( data, var, group = NULL, weight = NULL, prop = "none", na.rm = TRUE )
data |
A data frame or data.table containing the variables to tabulate. |
var |
Character string specifying the name of the variable to tabulate. |
group |
Character vector of variable name(s) to group by. If |
weight |
Character string specifying the name of the weight variable to use for weighted frequencies. |
prop |
Character string specifying proportion type. Options are |
na.rm |
Should missing values be removed from the table? Defaults to TRUE. |
A data.table with the following structure:
item |
Variable name of the tabulated variable |
label |
Variable label from the |
var |
Factor levels of the tabulated variable |
n |
Weighted counts |
total |
Overall proportions (if |
\link{group} level |
One column per level of each grouping variable (if |
compose_table(penguins, var = "species", group = "island", prop = "col")compose_table(penguins, var = "species", group = "island", prop = "col")
Calculates adjusted standardized residuals for a contingency table formed by crossing a row variable with one or more grouping (column) variables. These residuals measure how much each observed cell frequency deviates from what would be expected under independence, adjusted for both row and column proportions.
compute_adj_residuals(data, var, group, weight)compute_adj_residuals(data, var, group, weight)
data |
A data frame or tibble containing the survey data. |
var |
Character string specifying the row variable name. |
group |
Character vector specifying one or more column grouping variable names. When multiple groups are provided, residuals are computed separately for each group's contingency table. |
weight |
Character string specifying the weight variable name. |
Adjusted standardized residuals are calculated as:
where is the observed frequency, is the expected frequency
under independence, is the row proportion, and
is the column proportion.
These residuals can be interpreted like z-scores: values beyond ±2 suggest
significant deviation from independence at approximately the 0.05 level.
They are equivalent to the standardized residuals from chisq.test().
When multiple grouping variables are provided, residuals are computed
independently for each two-way contingency table (var × group_i), then
combined into a single output table matching the structure of
compose_table().
A data.table with the same structure as the output from
compose_table() with the same grouping variables. The first column
contains the row variable levels, and subsequent columns contain adjusted
standardized residuals for each group level. When multiple groups are
specified, columns are prefixed with [group_name] to match
compose_table() output.
## Not run: # Single group compute_adj_residuals(cvvm, var = "t_VZD", group = "IDE_8", weight = "soc_weight") # Multiple groups compute_adj_residuals( cvvm, var = "t_VZD", group = c("t_VEK_6", "IDE_8"), weight = "soc_weight" ) ## End(Not run)## Not run: # Single group compute_adj_residuals(cvvm, var = "t_VZD", group = "IDE_8", weight = "soc_weight") # Multiple groups compute_adj_residuals( cvvm, var = "t_VZD", group = c("t_VEK_6", "IDE_8"), weight = "soc_weight" ) ## End(Not run)
Computes weighted frequency tables for a variable, optionally grouped by another variable. Can return counts or proportions calculated across different margins (total, row, or column).
count_freqs( data, var, group = NULL, weight = NULL, prop = "none", na.rm = TRUE, include_empty = TRUE )count_freqs( data, var, group = NULL, weight = NULL, prop = "none", na.rm = TRUE, include_empty = TRUE )
data |
A data frame or object that can be coerced to a data.table. |
var |
Character string specifying the name of the variable to tabulate. |
group |
Character string specifying the name of the grouping variable,
or |
weight |
Character string specifying the name of the weight variable to use for weighted frequencies. If no weights are specified, equal weights are assumed. |
prop |
Character string indicating the type of proportion to calculate. Options are:
|
na.rm |
Logical. If |
include_empty |
Logical. If |
A data.table containing:
When group is NULL: A table with var and either n (counts) or
total (proportions)
When group is specified and prop != "none": A wide-format table with
var as rows and group levels as columns, containing proportions
When group is specified and prop == "none": A long-format table with
var, group, and n columns
count_freqs(data = penguins, var = "species", prop = "none") count_freqs(data = penguins, var = "species", group = "island", prop = "col")count_freqs(data = penguins, var = "species", prop = "none") count_freqs(data = penguins, var = "species", group = "island", prop = "col")
Extracts the names of factor variables from a data frame. Optionally includes character variables and allows exclusion of specific variable names.
get_categorical_vars(data, exclude = NULL, include_char = FALSE)get_categorical_vars(data, exclude = NULL, include_char = FALSE)
data |
A data frame to extract categorical variable names from. |
exclude |
A character vector of variable names to exclude from the results.
Default is |
include_char |
Logical. If |
A character vector of variable names.
# Get only factor variables get_categorical_vars(iris) # Include character variables get_categorical_vars(mtcars, include_char = TRUE) # Exclude specific variables get_categorical_vars(iris, exclude = c("Species"))# Get only factor variables get_categorical_vars(iris) # Include character variables get_categorical_vars(mtcars, include_char = TRUE) # Exclude specific variables get_categorical_vars(iris, exclude = c("Species"))