tab_many | R Documentation |
A full-featured function to create, manipulate and format many cross-tables
as one, using colors to make the printed tab more easily readable (in R terminal or
exported to Excel with tab_xl
).
Since objects of class tabxplor_tab
are also of class tibble
, you can then use all
dplyr verbs to modify the result, like select
,
arrange
, filter
or mutate
.
Only breaks for attractions/over-representations (in green) should be given, as a vector of positive doubles, with length between 1 and 5. Breaks for aversions/under-representations (in orange/red) will simply be the opposite.
tab_many(
data,
row_vars,
col_vars,
tab_vars,
wt,
pct = "no",
color = "no",
OR = "no",
chi2 = FALSE,
na = "keep",
levels = "all",
na_drop_all,
cleannames = NULL,
compact = NULL,
other_if_less_than = 0,
other_level = "Others",
ref = "auto",
ref2 = "first",
comp = "tab",
ci = "no",
conf_level = 0.95,
method_cell = "wilson",
method_diff = "ac",
totaltab = "line",
totaltab_name = "Ensemble",
totrow = TRUE,
totcol = "last",
total_names = "Total",
add_n = TRUE,
add_pct = FALSE,
digits = 0,
subtext = "",
filter
)
tab_get_vars(tabs, vars = c("row_var", "col_vars", "tab_vars"))
is_tab(x)
set_color_style(
type = c("text", "bg"),
theme = NULL,
html_24_bit = c("blue_red", "green_red", "no"),
custom_palette = NULL
)
get_color_style(
mode = c("crayon", "color_code"),
type = NULL,
theme = NULL,
html_24_bit = NULL
)
set_color_breaks(pct_breaks, mean_breaks, contrib_breaks)
get_color_breaks(brk, type = c("positive", "all"))
data |
A data frame. |
row_vars |
The row variable, which will be printed with one level per line. If numeric, it will be converted to factor. If more than one row_var if provided, a different table is made for each of them. |
col_vars |
<tidy-select>
One column is printed for each level of each column variable.
For numeric variables means are calculated, in a single column.
To pass many variables you may use syntax |
tab_vars |
<tidy-select>
One subtable is made for each combination of levels of the tab variables.
To pass many variables you may use syntax |
wt |
A weight variable, of class numeric. Leave empty for unweighted results. |
pct |
The type of percentages to calculate :
The argument is vectorised over both |
color |
The type of colors to print, as a single string. Vectorised over
|
OR |
With
|
chi2 |
Set to |
na |
The policy to adopt with missing values. It must be a single string.
|
levels |
The levels of
|
na_drop_all |
<tidy-select>
Removes all observations with a |
cleannames |
Set to |
compact |
With several |
other_if_less_than |
When set to a positive integer, levels with less count than it will be merged into an "Others" level. |
other_level |
The name of the "Other" level, as a single string. |
ref |
The reference cell to calculate differences and ratios
(used to print
|
ref2 |
A second reference cell is needed to calculate odds ratios
(or relative risks ratios). The first cell of the row or column is used by default.
See |
comp |
The comparison level : by subtables/groups, or for the whole table.
Vectorised over
|
ci |
The type of confidence intervals to calculate, passed to
By default, for percentages, with |
conf_level |
The confidence level, as a single numeric between 0 and 1. Default to 0.95 (95%). |
method_cell |
Character string specifying which method to use with percentages
for |
method_diff |
Character string specifying which method to use with percentages
for |
totaltab |
The total table, if there are subtables/groups
(i.e. when
|
totaltab_name |
The name of the total table, as a single string. |
totrow |
By default, total rows are printed.
Set to |
totcol |
The policy with total columns. Vectorised over
|
total_names |
The names of the totals, as a character vector of length one or two.
Use syntax of type |
add_n |
For |
add_pct |
Set to |
digits |
The number of digits to print, as a single integer, or an integer vector
the same length as |
subtext |
A character vector to print rows of legend under the table. |
filter |
A |
tabs |
A |
vars |
In |
x |
A object to test with |
type |
Default to |
theme |
For |
html_24_bit |
Use 24bits colors palettes for html tables : set to |
custom_palette |
Possibility to provide a custom color styles, as a character
vector of 10 html color codes (the five first for over-represented numbers,
the five last for under-represented ones). The result is saved to
|
mode |
By default, |
pct_breaks |
If they are to be changed, the breaks used for percentages.
Default to |
mean_breaks |
If they are to be changed, the breaks used for means.
Default to |
contrib_breaks |
If they are to be changed, the breaks used for contributions to
variance. Default to |
brk |
When missing, return all color breaks. Specify to return a given color
break, among |
A tibble
of class tab
, possibly with colored reading helpers.
When there are two row_vars
or more, a list of tibble
of class tab
.
All non-text columns are of class fmt
, storing all
the data necessary to print formats and colors. Columns with row_var
and
tab_vars
are of class factor
: every added factor
will be
considered as a tab_vars
and used for grouping. To add text columns without
using them in calculations, be sure they are of class character
.
A list with the variables names.
A single logical.
Set global options "tabxplor.color_style_type"
and
"tabxplor.color_style_theme"
, used when printing tab
objects.
A vector of crayon color functions, or a vector of color html codes.
Set the global option "tabxplor.color_breaks" as a list different double vectors, and also returns it invisibly.
The color breaks as a double vector, or list of double vectors.
tab_get_vars()
: Get the variables names of a tabxplor tab
is_tab()
: a test function for class tabxplor_tab
set_color_style()
: define the color style used to print tab
.
get_color_style()
: get color styles as crayon functions or html codes.
set_color_breaks()
: set the breaks used to print colors
get_color_breaks()
: get the breaks currently used to print colors
# Make a summary table with many col_vars, showing only one specific level :
library(dplyr)
first_lvs <- c("Married", "$25000 or more", "Strong republican", "Protestant")
data <- forcats::gss_cat %>% mutate(across(
where(is.factor),
~ forcats::fct_relevel(., first_lvs[first_lvs %in% levels(.)])
))
tab_many(data, race, c(marital, rincome, partyid, relig, age, tvhours),
levels = "first", pct = "row", chi2 = TRUE, color = "auto")
# Can be used with map and tribble to program several tables with different parameters
# all at once, in a readable way:
library(purrr)
library(tibble)
pmap(
tribble(
~row_var, ~col_vars , ~pct , ~filter , ~subtext ,
"race" , "marital" , "row", NULL , "Source: GSS 2000-2014",
"relig" , c("race", "age"), "row", "year %in% 2000:2010", "Source: GSS 2000-2010",
NA_character_, "race" , "no" , NULL , "Source: GSS 2000-2014",
),
.f = tab_many,
data = forcats::gss_cat, color = "auto", chi2 = TRUE)
set_color_style(type = "bg")
set_color_breaks(
pct_breaks = c(0.05, 0.15, 0.3),
mean_breaks = c(1.15, 2, 4),
contrib_breaks = c(1, 2, 5)
)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.