Title: | User-Friendly Tables with Color Helpers for Data Exploration |
Version: | 1.3.0 |
Description: | Make it easy to deal with multiple cross-tables in data exploration, by creating them, manipulating them, and adding color helpers to highlight important informations (differences from totals, comparisons between lines or columns, contributions to variance, confidence intervals, odds ratios, etc.). All functions are pipe-friendly and render data frames which can be easily manipulated. In the same time, time-taking operations are done with 'data.table' to go faster with big dataframes. Tables can be exported with formats and colors to 'Excel', plot and html. |
URL: | https://github.com/BriceNocenti/tabxplor |
BugReports: | https://github.com/BriceNocenti/tabxplor/issues |
License: | GPL (≥ 3) |
Encoding: | UTF-8 |
RoxygenNote: | 7.3.2 |
Suggests: | fansi (≥ 0.5.0), htmltools (≥ 0.5.0), jmvcore (≥ 2.4.0), knitr, openxlsx (≥ 4.0.0), R6, ggpubr (≥ 0.6.0), ggplot2 (≥ 3.4.0), cowplot (≥ 1.1.1), gtable, grDevices, grid, rmarkdown, rstudioapi (≥ 0.1), testthat (≥ 3.0.0), labelled (≥ 2.0.0) |
Config/testthat/edition: | 3 |
Imports: | dplyr (≥ 1.0.3), stringr (≥ 1.4.0), crayon (≥ 1.3.0), forcats (≥ 0.5.0), magrittr (≥ 1.5.0), purrr (≥ 0.3.0), rlang (≥ 0.4.0), tibble (≥ 3.1.0), tidyr (≥ 1.1.0), vctrs (≥ 0.3.0), cli (≥ 2.0.0), tidyselect (≥ 1.0.0), stringi (≥ 1.4.6), pillar (≥ 1.6.0), stats (≥ 4.0.0), kableExtra (≥ 1.3.0), DescTools (≥ 0.99.0), data.table |
Depends: | R (≥ 4.1.0) |
VignetteBuilder: | knitr |
Config/potools/style: | explicit |
NeedsCompilation: | no |
Packaged: | 2025-03-09 17:01:10 UTC; Brice |
Author: | Brice Nocenti [aut, cre] |
Maintainer: | Brice Nocenti <brice.nocenti@gmail.com> |
Repository: | CRAN |
Date/Publication: | 2025-03-09 17:30:02 UTC |
$ method for class tabxplor_fmt
Description
$ method for class tabxplor_fmt
Usage
## S3 method for class 'tabxplor_fmt'
x$name
Arguments
x |
A tabxplor_fmt object. |
name |
The name of the field to extract. |
Value
The relevant field of the tabxplor_fmt.
Pipe operator
Description
See magrittr::%>%
for details.
Usage
lhs %>% rhs
Arguments
lhs |
A value or the magrittr placeholder. |
rhs |
A function call using the magrittr semantics. |
Value
Pipe an object forward into a function or call expression.
set subset method for class tabxplor_grouped_tab
Description
set subset method for class tabxplor_grouped_tab
Usage
"x[i] <- value ; x[i, j, ...] <- value"
Arguments
x |
A tabxplor_grouped_tab object. |
i , j , ... |
Indices. |
value |
The new value. |
Value
An object of class tabxplor_grouped_tab
.
subset method for class tabxplor_grouped_tab
Description
subset method for class tabxplor_grouped_tab
Usage
"x[i] ; x[i, j, ... , drop = TRUE]"
Arguments
x |
A tabxplor_grouped_tab object. |
i , j , ... |
Indices |
drop |
For matrices and arrays. If TRUE the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. |
Value
An object of class tabxplor_grouped_tab
.
set sub-subset method for class tabxplor_grouped_tab
Description
set sub-subset method for class tabxplor_grouped_tab
Usage
"x[[...]] <- value"
Arguments
x |
A tabxplor_grouped_tab object. |
... |
Indices |
value |
The new value. |
Value
An object of class tabxplor_grouped_tab
.
arrange method for class tabxplor_tab
Description
arrange method for class tabxplor_tab
Usage
## S3 method for class 'tabxplor_tab'
arrange(
.data,
...,
.by_group = TRUE,
.by_totals = TRUE,
.only_main_display = TRUE,
.locale = NULL
)
Arguments
.data |
A tibble of class tabxplor_tab. |
... |
< |
.by_group |
By default, will sort first by grouping variable.
Set to |
.by_totals |
By default, will put totals at the end of their group.
Set to |
.only_main_display |
By default, only the rows with the same display
than the first row are arranged : if the first row of the group displays
percentages, rows with n or pvalues are kept at the same place
(typically, at the end of the group). Rows with the text |
.locale |
The locale to sort character vectors in. |
Value
A tibble of class tabxplor__tab
or tabxplor_grouped_tab
.
Bind dataframes for tab / tab_many
Description
Bind dataframes for tab / tab_many
Usage
bind_datas_for_tab(data, vars)
Arguments
data |
Dataframes to be bound by rows. |
vars |
Selected variables. |
Value
A tibble.
A regex pattern to clean the names of factors.
Description
A regex pattern to clean the names of factors.
Usage
cleannames_condition()
Compare levels of factors in many df
Description
Compare levels of factors in many df
Usage
compare_levels(data, vars = c("var1", "var2"))
Arguments
data |
Data to use. |
vars |
Variables to compare levels. |
Value
A list with results.
Complete partial total rows
Description
Complete partial total rows
Usage
complete_partial_totals(tabs)
Arguments
tabs |
A table or data.framate containting |
Value
The table with completed total rows, total tables, and reference rows.
dplyr_col_modify method for class tabxplor_grouped_tab
Description
dplyr_col_modify method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
dplyr_col_modify(data, cols)
Arguments
data |
A data frame. |
cols |
A named list used modify columns. A |
Value
An object of class tabxplor_grouped_tab
.
dplyr_reconstruct method for class tabxplor_grouped_tab
Description
dplyr_reconstruct method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
dplyr_reconstruct(data, template)
Arguments
data |
A data frame. |
template |
Template to use for restoring attributes |
Value
An object of class tabxplor_grouped_tab
.
dplyr_row_slice method for class tabxplor_grouped_tab
Description
dplyr_row_slice method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
dplyr_row_slice(data, i, ...)
Arguments
data |
A data frame. |
i |
A numeric or logical vector that indexes the rows of |
... |
Future parameters. |
Value
An object of class tabxplor_grouped_tab
.
Recode Factor Levels with Multiple Patterns Detection
Description
Recode Factor Levels with Multiple Patterns Detection
Usage
fct_case_when_recode(
factor,
pattern_replacement_named_vector,
.else = levels(factor)
)
Arguments
factor |
A factor. |
pattern_replacement_named_vector |
A named character vector, with regular expressions to find in values, replacements in names. |
.else |
A character vector of length 1 to rename factor levels detected with no pattern. |
Value
A factor.
Clean factor levels.
Description
Clean factor levels.
Usage
fct_clean(factor, pattern = cleannames_condition())
Arguments
factor |
A factor. |
pattern |
A pattern. |
Value
A factor.
Recode Factor Levels with Detected Pattern inside
Description
Recode factor levels using str_detect
.
Usage
fct_detect_replace(factor, pattern, replacement, negate = FALSE)
Arguments
factor |
A factor. |
pattern |
A character vector of length 1. |
replacement |
A character vector of length 1. |
negate |
A factor. |
Value
A factor.
Copy level of factors between dataframes
Description
Based on the prefix numbers, otherwise don't work.
Usage
fct_levels_from_vector(data_to, data_from, var)
Arguments
data_to |
Data with the variable with levels to change. |
data_from |
Data with the variable with good levels |
var |
The variable : must exist on both df. |
Value
A factor.
fct_recode helper to recode multiple variables
Description
fct_recode helper to recode multiple variables
Usage
fct_recode_helper(
data,
.cols = -where(is.numeric),
name_in,
name_out,
style = c("mutate", "base"),
reminder = TRUE,
cat = TRUE
)
Arguments
data |
The data frame. |
.cols |
<tidy-select> The variables to recode. |
name_in |
The name of the input data frame. Default to the expression given in |
name_out |
The name of the output data frame, if different from the input data frame. |
style |
Default is to use |
reminder |
By default, a reminder of the syntax ( |
cat |
By default the result is written in the console if there are less than 6 variables, written in a temporary file and opened otherwise. Set to false to get a data frame with a character variable instead. |
Value
When the number of variables is less than 5, a text in console as a side effect.
With more than 5 variables, a temporary R file. A tibble
with the recode text as a
character variable is returned invisibly (or as main result if cat = TRUE
).
If the labelled
package in installed, the variable label is used as title in a comment.
Recode Factor Levels using Multiple Patterns
Description
Recode Factor Levels using Multiple Patterns
Usage
fct_rename(factor, pattern_replacement_named_vector)
Arguments
factor |
A factor. |
pattern_replacement_named_vector |
A named character vector, with regular expressions to find in values, replacements in names. |
Value
A factor.
Recode Factor Levels using one Pattern
Description
Recode factor levels using str_replace_all
.
Usage
fct_replace(factor, pattern, replacement)
Arguments
factor |
A factor. |
pattern |
A character of length 1. |
replacement |
A character of length 1. |
Value
A factor
Replace Factor Levels with NA
Description
Replace Factor Levels with NA
Usage
fct_to_na(factor, patternlist)
Arguments
factor |
A factor. |
patternlist |
A character vector of levels. |
Value
A factor.
Create a vector of class formatted numbers
Description
fmt
vectors, of class tabxplor_fmt
, powers tabxplor
and tab
tibbles.
As a record
, they stores all data necessary to
calculate percentages, Chi2 metadata or confidence intervals, but also to format and
color the table to help the user read it. You can access this data with
vctrs::field
, or change it with
vctrs:field<-
. A fmt
vector have 13 fields :
n
, digits
, display
, wn
, pct
, mean
,
diff
, ctr
, var
, ci
, in_totrow
, in_tottab
,
in_refrow
. Other arguments are attributes, attached not to each value, but to
the whole vector, like type
, totcol
or color
. You can get them
with attr
and modify them with
attr<-
. Special functions listed below are made to
facilitate programming with with tabxplor formatted numbers.
taxplfmt
vectors can use all standard operations, like +, -, sum(), or c(),
using vctrs.
Usage
fmt(
n = integer(),
type = "n",
digits = rep(0L, length(n)),
display = dplyr::case_when(type == "mean" ~ "mean", type %in% c("row", "col", "all",
"all_tabs") ~ "pct", TRUE ~ "n"),
wn = rep(NA_real_, length(n)),
pct = rep(NA_real_, length(n)),
mean = rep(NA_real_, length(n)),
diff = rep(NA_real_, length(n)),
ctr = rep(NA_real_, length(n)),
var = rep(NA_real_, length(n)),
ci = rep(NA_real_, length(n)),
rr = rep(NA_real_, length(n)),
or = rep(NA_real_, length(n)),
in_totrow = rep(FALSE, length(n)),
in_tottab = rep(FALSE, length(n)),
in_refrow = rep(FALSE, length(n)),
comp_all = NA,
ref = "",
ci_type = "",
col_var = "",
totcol = FALSE,
refcol = FALSE,
color = ""
)
is_fmt(x)
get_num(x)
set_num(x, value)
get_type(x, ...)
set_type(x, type)
is_totrow(x, ...)
as_totrow(x, in_totrow = TRUE)
is_tottab(x, ...)
as_tottab(x, in_tottab = TRUE)
set_display(x, value)
is_totcol(x, ...)
as_totcol(x, totcol = TRUE)
is_refrow(x, ...)
as_refrow(x, in_refrow = TRUE)
get_comp_all(x, replace_na = TRUE)
set_comp_all(x, comp_all = FALSE)
get_ref_type(x, ...)
set_diff_type(x, ref)
get_ci_type(x, ...)
set_ci_type(x, ci_type)
get_col_var(x, ...)
set_col_var(x, col_var)
is_refcol(x, ...)
as_refcol(x, refcol = TRUE)
get_color(x, ...)
set_color(x, color)
get_digits(x)
set_digits(x, value)
Arguments
n |
The underlying count, as an integer vector of length |
type |
The type of the column, which defines the type of background calculation to be made (as a single string, since it's not a field but an attribute) :
|
digits |
The number of digits, as an integer, or an integer vector the length
of |
display |
The display type : the name of the field you want to show when printing
the vector. Among |
wn |
The underlying weighted counts, as a double vector the length of
|
pct |
The percentages, as a double vector the length of |
mean |
The means, as a double vector the length of |
diff |
The differences (from totals or first cells),
as a double vector the length of |
ctr |
The contributions of cells to (sub)tables variances,
as a double vector the length of |
var |
The cells variances, as a double vector the length of |
ci |
The confidence intervals, as a double vector the length of |
rr |
The relative risk, as a double vector the length of |
or |
The odds ratio or relative risk ratio, as a double vector the length of |
in_totrow |
|
in_tottab |
|
in_refrow |
|
comp_all |
|
ref |
The type of difference of the vector. Cf. |
ci_type |
The type of confidence intervals of the vector (calculate
with
|
col_var |
The name of the |
totcol |
|
refcol |
|
color |
The type of color to print :
|
x |
The object to test, to get a field in, or to modify. |
value |
The value you want to inject in some |
... |
Used in methods to add arguments in the future. |
replace_na |
By default, |
Value
A vector of class tabxplor_fmt
.
A logical vector.
A double vector.
A modified fmt vector.
A character vector with the vectors type.
A modified fmt vector.
A logical vector with the fmt vectors totrow field.
A modified fmt vector with totrow field changed.
A logical vector with the fmt vectors tottab field.
A modified fmt vector with tottab field changed.
The entered objects, with all fmt vectors with the wanted display.
A logical vector with the fmt vectors totcol attribute.
A modified fmt vector with totcol attribute changed.
A logical vector with the fmt vectors in_refrow field
A modified fmt vector with in_refrom field changed.
A modified fmt vector with comp attribute changed.
A logical vector with the fmt vectors type attributes
A modified fmt vector.
A logical vector with the fmt vectors ci_type attributes
A modified fmt vector.
A logical vector with the fmt vectors col_var attributes
A modified fmt vector.
A logical vector with the fmt vectors is_refcol attributes
A modified fmt vector.
A logical vector with the fmt vectors color attributes
A modified fmt vector.
Functions
-
is_fmt()
: a test function for class fmt. -
get_num()
: get the currently displayed field -
set_num()
: set the currently displayed field (not changing display type) -
get_type()
: get types of fmt columns (atfmt
level ortab
level) -
set_type()
: set the column type attribute of afmt
vector -
is_totrow()
: test function to detect cells in total rows (atfmt
level ortab
level) -
as_totrow()
: set the "in_totrow" field (belong to total row) -
is_tottab()
: test function to detect cells in total tables (atfmt
level ortab
level) -
as_tottab()
: set the "in_tottab" field (belong to total table) -
set_display()
: set the "display" vctrs::field of afmt
vector, or of all of them in the whole tibble. -
is_totcol()
: test function for total columns (atfmt
level ortab
level) -
as_totcol()
: set the "totcol" attribute of afmt
vector -
is_refrow()
: test function to detect cells in reference rows (atfmt
level ortab
level) -
as_refrow()
: set the "in_refrow" field (belong to reference row) -
get_comp_all()
: get comparison level of fmt columns -
set_comp_all()
: set the comparison level attribute of afmt
vector -
get_ref_type()
: get differences type of fmt columns (atfmt
level ortab
level) -
set_diff_type()
: set the differences type attribute of afmt
vector -
get_ci_type()
: get confidence intervals type of fmt columns (atfmt
level ortab
level) -
set_ci_type()
: set the confidence intervals type attribute of afmt
vector -
get_col_var()
: get names of column variable of fmt columns (atfmt
level ortab
level) -
set_col_var()
: set the "col_var" attribute of afmt
vector -
is_refcol()
: test function for reference columns (atfmt
level ortab
level) -
as_refcol()
: set the "ref_col" attribute of afmt
vector -
get_color()
: get color (atfmt
level ortab
level) -
set_color()
: set the "color" attribute of afmt
vector -
get_digits()
: get the "digits" field -
set_digits()
: set the "digits" field
Examples
library(dplyr)
f <- fmt(n = c(7, 19, 2), type = "row", pct = c(0.25, 0.679, 0.07))
f
# To get the currently displayed field :
get_num(f)
# To modify the currently displayed field :
set_num(f, c(1, 0, 0))
# See all the underlying fields of a fmt vector (a data frame with a number of rows
# equal to the length of the vector) :
vctrs::vec_data(f)
# To get the numbers of digits :
vctrs::field(f, "digits")
f$digits
# To get the count :
vctrs::field(f, "n")
f$n
# To get the display :
vctrs::field(f, "display")
f$display
# To modify a field, you can use `dplyr::mutate` on the fmt vector,
# referring to the names of the columns of the underlying data.frame (`vctrs::vec_data`) :
vctrs::`field<-`(f, "pct", c(1, 0, 0))
mutate(f, pct = c(1, 0, 0))
# See all the attributes of a fmt vector :
attributes(f)
# To modify the "type" attribute of a fmt vector :
set_type(f, "col")
# To modify the "color" attribute of a fmt vector :
set_color(f, "contrib")
tabs <- tab(starwars, sex, hair_color, gender, na = "drop", pct = "row",
other_if_less_than = 5)
# To identify the total columns, and work with them :
is_totcol(tabs)
tabs %>% mutate(across(where(is_totcol), ~ "total column"))
# To identify the total rows, and work with them :
is_totrow(tabs)
tabs %>%
mutate(across(
where(is_fmt),
~ if_else(is_totrow(.), true = "into_total_row", false = "normal_cell")
))
# To identify the total tables, and work with them :
tottabs <- is_tottab(tabs)
tabs %>% tibble::add_column(tottabs) %>%
mutate(total = if_else(tottabs, "part of a total table", "normal cell"))
# To access the displayed numbers, as numeric vectors :
tabs %>% mutate(across(where(is_fmt), get_num))
# To access the displayed numbers, as character vectors (without colors) :
tabs %>% mutate(across(where(is_fmt), format))
# To access the (non-displayed) differences of the cells percentages from totals :
tabs %>% mutate(across(where(is_fmt), ~ vctrs::field(., "diff")))
# To do more complex operations, like creating a new column with standard deviation and
# print it with 2 decimals, use `dplyr::mutate` on all the fmt columns of a table :
tab_num(forcats::gss_cat, race, c(age, tvhours), marital, digits = 1L, comp = "all") |>
dplyr::mutate(dplyr::across( #Mutate over the whole table.
c(age, tvhours),
~ dplyr::mutate(., #Mutate over each fmt vector's underlying data.frame.
var = sqrt(var),
display = "var",
digits = 2L) |>
set_color("no"),
.names = "{.col}_sd"
))
Get HTML Color Code of a fmt vector
Description
Get HTML Color Code of a fmt vector
Usage
fmt_get_color_code(x, type = "text", theme = "light", html_24_bit = NULL)
Arguments
x |
The fmt vector to get the html color codes from. |
type |
The style type in |
theme |
For |
html_24_bit |
Use 24bits colors palettes for html tables : set to |
Value
A character vector with html color codes, of the length of the initial vector.
Examples
tabs <- tab(forcats::gss_cat, race, marital, pct = "row", color = "diff")
dplyr::mutate(tabs, across(where(is_fmt), fmt_get_color_code))
Print method for class tabxplor_fmt
Description
Print method for class tabxplor_fmt
Usage
## S3 method for class 'tabxplor_fmt'
format(x, ..., html = FALSE, na = NA, special_formatting = FALSE)
Arguments
x |
A fmt object. |
... |
Other parameters. |
html |
Should html tags be added (to print confidence intervals as subscripts) ? |
na |
How |
special_formatting |
Set to |
Value
The fmt printed in a character vector.
INSEE SAS formats to R : translate code
Description
INSEE SAS formats to R : translate code
Usage
formats_SAS_to_R(
path,
name_in,
name_out,
open = TRUE,
remove_at_end_of_var = "f",
not_if_numeric = TRUE,
text_aposthophe = "'",
path_out
)
Arguments
path |
The path of the file with the sas formats |
name_in |
The name of the unformatted database |
name_out |
The name of the database to be formatted, if not the same than |
open |
Should the file be opened, or just its path printed ? |
remove_at_end_of_var |
Set to |
not_if_numeric |
Should the code prevent numeric variables to get recoded ? |
text_aposthophe |
How do apostrophes in labels appear ? |
path_out |
The path, name and extension of the output file. In temporary directory if not provide. |
Value
A file with R code.
Get confidence intervals type of fmt columns
Description
Get confidence intervals type of fmt columns
Usage
## S3 method for class 'data.frame'
get_ci_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A character vector with the ci_type attributes.
Get confidence intervals type of fmt columns
Description
Get confidence intervals type of fmt columns
Usage
## Default S3 method:
get_ci_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the ci_type attribute.
Get confidence intervals type of fmt columns
Description
Get confidence intervals type of fmt columns
Usage
## S3 method for class 'tabxplor_fmt'
get_ci_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the ci_type attribute.
Get names of column variable of fmt columns
Description
Get names of column variable of fmt columns
Usage
## S3 method for class 'data.frame'
get_col_var(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A character vector with the col_var attributes.
Get names of column variable of fmt columns
Description
Get names of column variable of fmt columns
Usage
## Default S3 method:
get_col_var(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the col_var attribute.
Get names of column variable of fmt columns
Description
Get names of column variable of fmt columns
Usage
## S3 method for class 'tabxplor_fmt'
get_col_var(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the col_var attribute.
Get color
Description
Get color
Usage
## S3 method for class 'data.frame'
get_color(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A character vector with the color attributes.
Get color
Description
Get color
Usage
## Default S3 method:
get_color(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the color attribute.
Get color
Description
Get color
Usage
## S3 method for class 'tabxplor_fmt'
get_color(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the color attribute.
get the "display" field of a fmt
vector
Description
get the "display" field of a fmt
vector
Usage
get_display(x)
Arguments
x |
The formatted number in which you want to find data for "get" functions, to modify data for "set" functions. |
Get differences type of fmt columns
Description
Get differences type of fmt columns
Usage
## S3 method for class 'data.frame'
get_ref_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A character vector with the ref attribute.
Get differences type of fmt columns
Description
Get differences type of fmt columns
Usage
## Default S3 method:
get_ref_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the ref attribute.
Get differences type of fmt columns
Description
Get differences type of fmt columns
Usage
## S3 method for class 'tabxplor_fmt'
get_ref_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the ref attribute.
Get types of fmt columns
Description
Get types of fmt columns
Usage
## S3 method for class 'data.frame'
get_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A character vector with the data.frame column's types.
Get types of fmt columns
Description
Get types of fmt columns
Usage
## Default S3 method:
get_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
An empty character vector.
Get types of fmt columns
Description
Get types of fmt columns
Usage
## S3 method for class 'tabxplor_fmt'
get_type(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single string with the vector's type.
group_by method for class tabxplor_tab
Description
group_by method for class tabxplor_tab
Usage
## S3 method for class 'tabxplor_tab'
group_by(.data, ..., .add = FALSE, .drop = dplyr::group_by_drop_default(.data))
Arguments
.data |
A tibble of class |
... |
Variables or computations to group by. |
.add |
When |
.drop |
Drop groups formed by factor levels that don't appear in the
data? The default is |
Value
A tibble of class tabxplor_grouped_tab
.
Test function for reference columns
Description
Test function for reference columns
Usage
## S3 method for class 'data.frame'
is_refcol(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A character vector with the ref_col attributes.
Test function for reference columns
Description
Test function for reference columns
Usage
## Default S3 method:
is_refcol(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the ref_col attribute.
Test function for reference columns
Description
Test function for reference columns
Usage
## S3 method for class 'tabxplor_fmt'
is_refcol(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single character with the ref_col attribute.
Test function to detect cells in reference rows
Description
Test function to detect cells in reference rows
Usage
## S3 method for class 'data.frame'
is_refrow(x, ..., partial = TRUE)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
partial |
Should partial reference rows be counted as reference rows ? Default to FALSE. |
Value
A list of logical vectors with the in_refrow fields.
Test function to detect cells in reference rows
Description
Test function to detect cells in reference rows
Usage
## Default S3 method:
is_refrow(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A logical vector with FALSE, the length of x.
Test function to detect cells in reference rows
Description
Test function to detect cells in reference rows
Usage
## S3 method for class 'tabxplor_fmt'
is_refrow(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A logical vector with the in_refrow field.
Test function for total columns
Description
Test function for total columns
Usage
## S3 method for class 'data.frame'
is_totcol(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A logical vector, with the data.frame column's totcol attributes.
Test function for total columns
Description
Test function for total columns
Usage
## Default S3 method:
is_totcol(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single logical vector with the totcol attribute
Test function for total columns
Description
Test function for total columns
Usage
## S3 method for class 'tabxplor_fmt'
is_totcol(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A single logical vector with the totcol attribute
Test function to detect cells in total rows
Description
Test function to detect cells in total rows
Usage
## S3 method for class 'data.frame'
is_totrow(x, ..., partial = FALSE)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
partial |
Should partial total rows be counted as total rows ? Default to FALSE. |
Value
A list of logical vectors, with the data.frame column's totrow fields.
Test function to detect cells in total rows
Description
Test function to detect cells in total rows
Usage
## Default S3 method:
is_totrow(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A logical vector with FALSE
.
Test function to detect cells in total rows
Description
Test function to detect cells in total rows
Usage
## S3 method for class 'tabxplor_fmt'
is_totrow(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A logical vector with the totrow field.
Test function to detect cells in total tables
Description
Test function to detect cells in total tables
Usage
## S3 method for class 'data.frame'
is_tottab(x, ..., partial = FALSE)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
partial |
Should partial total tabs be counted as total tabs ? Default to FALSE. |
Value
A list of logical vectors, with the data.frame column's tottab fields.
Test function to detect cells in total tables
Description
Test function to detect cells in total tables
Usage
## Default S3 method:
is_tottab(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A logical vector with FALSE
.
Test function to detect cells in total tables
Description
Test function to detect cells in total tables
Usage
## S3 method for class 'tabxplor_fmt'
is_tottab(x, ...)
Arguments
x |
The object to test, to get a field in, or to modify. |
... |
Used in methods to add arguments in the future. |
Value
A logical vector with the tottab field.
Crosstables
Description
Crosstables
Usage
jmvtab(
data,
row_vars = NULL,
col_vars = NULL,
tab_vars = NULL,
wt = NULL,
pct = "no",
color = "no",
chi2 = TRUE,
OR = "no",
na = "keep",
lvs = "all",
other_if_less_than = 0,
cleannames = TRUE,
ref = "auto",
ref2 = "first",
comp = "tab",
ci = "auto",
conf_level = 0.95,
ci_print = "ci",
totaltab = "line",
wrap_rows = 35,
wrap_cols = 15,
display = "auto",
add_n = TRUE,
add_pct = FALSE,
subtext = "",
digits = 0
)
Arguments
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. |
col_vars |
One column is printed for each level of each column variable. For numeric variables means are calculated, in a single column. |
tab_vars |
One subtable is made for each combination of levels of the tab variables. All tab variables are converted to factor. Leave empty to make a simple table. |
wt |
A weight variable, of class numeric. Leave empty for unweighted results. |
pct |
The type of percentages to calculate :
|
color |
The type of colors to print, as a single string. Vectorised
over
|
chi2 |
Set to |
OR |
With
|
na |
The policy to adopt with missing values. It must be a single string.
|
lvs |
The levels of
|
other_if_less_than |
When set to a positive integer, levels with less count than that will be merged into an "Others" level. |
cleannames |
By default, clean levels names, by removing prefix
numbers like "1-", and text in parenthesis. Set to |
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. |
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
|
ci_print |
By default confidence interval are printed with the interval display. Set to "moe" to use pct +- moe instead. |
totaltab |
The total table, if there are subtables/groups (i.e. when
|
wrap_rows |
By default, rownames are wrapped when larger than 30 characters. |
wrap_cols |
By default, colnames are wrapped when larger than 12 characters. |
display |
The information to display in the table. |
add_n |
For |
add_pct |
Set to |
subtext |
A character vector to print rows of legend under the table. |
digits |
The number of digits to print, as a single integer, or an
integer vector the same length as |
Value
A results object containing:
results$html_table | a html | ||||
results$chi2_table | a table | ||||
results$plot | an image | ||||
Tables can be converted to data frames with asDF
or as.data.frame
. For example:
results$chi2_table$asDF
as.data.frame(results$chi2_table)
Print a tabxplor table in html
Description
Print a tabxplor table in html
Usage
kable_tabxplor_style(
tabs,
caption = knitr::opts_current$get("tab.cap"),
theme = c("light", "dark"),
total_in_bold = TRUE,
all_column_borders = FALSE,
html_font = NULL,
full_width = FALSE,
wrap_rows = 35,
wrap_cols = 15,
whitespace_only = TRUE,
subtext = "",
...
)
Arguments
tabs |
A data.frame. |
caption |
The table caption. For formatting, you need to use a |
theme |
By default, a white table with black text, Set to |
total_in_bold |
Should rows and cols with "Total" string be set in bold ? |
all_column_borders |
Put a vertical border around each column ? |
html_font |
A string for HTML css font. By default, it uses
|
full_width |
A TRUE or FALSE variable controlling whether the HTML table should have the preferable format for full_width. If not specified, a HTML table will have full width by default but this option will be set to FALSE for a LaTeX table. |
wrap_rows |
By default, rownames are wrapped when larger than 30 characters. |
wrap_cols |
By default, colnames are wrapped when larger than 12 characters. |
whitespace_only |
Set to |
subtext |
A character vector to print rows of legend under the table. |
... |
Other arguments to pass to |
Value
A html table (opened in the viewer in RStudio). Differences from totals, confidence intervals, contribution to variance, and unweighted counts, are available in an html tooltip at cells hover.
Examples
tabs <- tibble::tibble(nm = c("First", "Second", "Total"),
column1 = c(1, 2, 3),
column2 = c(4, 5, 6) )
kable_tabxplor_style(tabs)
A 2 arguments map_if
Description
A 2 arguments map_if
Usage
map2_if(.x, .y, .p, .f, ..., .else = NULL)
Arguments
.x , .y |
Lists. |
.p |
Predicate. |
.f |
Function if TRUE. |
... |
Other parameter to pass to the function. |
.else |
Function if FALSE. |
Value
A list of the same length.
mutate method to access vctrs::fields of tabxplor_fmt vectors
Description
mutate method to access vctrs::fields of tabxplor_fmt vectors
Usage
## S3 method for class 'tabxplor_fmt'
mutate(.data, ...)
Arguments
.data |
A tabxplor_fmt column. |
... |
Name-value pairs. The name gives the name of the column in the output (do not change it). The value can be:
|
Value
An object of class tabxplor_fmt
.
fmt a constructor for class fmt.
Description
fmt a constructor for class fmt.
Usage
new_fmt(
n = integer(),
type = "n",
digits = rep(0L, length(n)),
display = dplyr::case_when(type == "mean" ~ "mean", type %in% c("row", "col", "all",
"all_tabs") ~ "pct", TRUE ~ "n"),
wn = rep(NA_real_, length(n)),
pct = rep(NA_real_, length(n)),
mean = rep(NA_real_, length(n)),
diff = rep(NA_real_, length(n)),
ctr = rep(NA_real_, length(n)),
var = rep(NA_real_, length(n)),
ci = rep(NA_real_, length(n)),
rr = rep(NA_real_, length(n)),
or = rep(NA_real_, length(n)),
in_totrow = rep(FALSE, length(n)),
in_tottab = rep(FALSE, length(n)),
in_refrow = rep(FALSE, length(n)),
comp_all = NA,
ref = "",
ci_type = "",
col_var = "",
totcol = FALSE,
refcol = FALSE,
color = "",
...,
class = character()
)
Arguments
class |
Subclasses to assign to the new object, default: none. |
A constructor for class tabxplor_tab
Description
A constructor for class tabxplor_tab
Usage
new_tab(
tabs = tibble::tibble(),
subtext = "",
chi2 = tibble::tibble(tables = character(), pvalue = double(), df = integer(), cells =
integer(), variance = double(), count = integer()),
...,
class = character()
)
new_grouped_tab(
tabs = tibble::tibble(),
groups,
subtext = "",
chi2 = tibble::tibble(tables = character(), pvalue = double(), df = integer(), cells =
integer(), variance = double(), count = integer()),
...,
class = character()
)
Arguments
tabs |
A table, stored into a |
subtext |
A character vector to print legend lines under the table. |
chi2 |
A tibble storing information about pvalues and variances, to fill with
|
... |
Needed to implement subclasses. |
class |
Needed to implement subclasses. |
groups |
The grouping data. |
Value
A tibble
of class tabxplor_tab
.
A tibble
of class tabxplor_grouped_tab
.
Print Chi2 tables columns
Description
Print Chi2 tables columns
Usage
## S3 method for class 'tab_chi2_fmt'
pillar_shaft(x, ...)
Arguments
x |
A fmt object. |
... |
Other parameter. |
Value
A Chi2 table column printed in a pillar.
Pillar_shaft method to print class fmt in a tibble
column
Description
Pillar_shaft method to print class fmt in a tibble
column
Usage
## S3 method for class 'tabxplor_fmt'
pillar_shaft(x, ...)
Arguments
x |
A fmt object. |
... |
Other parameter. |
Value
A fmt printed in a pillar.
A generalized map_if
Description
A generalized map_if
Usage
pmap_if(.l, .p, .f, ..., .else = NULL)
Arguments
.l |
List of lists. |
.p |
Predicate. |
.f |
Function if TRUE. |
... |
Other parameter to pass to the function. |
.else |
Function if FALSE. |
Value
A list of same length.
Prepare fct_recode
Description
Prepare fct_recode
Usage
prepare_fct_recode(
df_in,
df_out,
var,
mode = c("text", "numbers", "numbers_vector"),
numbers,
text
)
Arguments
df_in |
The name of the unformatted database |
df_out |
The name of the database to be formatted. |
var |
The name of the variable. |
mode |
"text", "numbers" or "numbers_vector" |
numbers |
If mode = "numbers", a character vector of length 1 with numbers. |
text |
The character vector of length 1 with text. |
Value
Code to be copied in console.
Printing method for class tabxplor_grouped_tab
Description
Printing method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
print(
x,
width = NULL,
...,
n = 100,
max_extra_cols = NULL,
max_footer_lines = NULL,
min_row_var = 30,
get_text = FALSE
)
Arguments
x |
Object to format or print. |
width |
Width of text output to generate. |
... |
Passed on to |
n |
Number of rows to show. |
max_extra_cols |
Number of extra columns to print abbreviated information for, if the width is too small for the entire tibble. |
max_footer_lines |
Maximum number of footer lines. |
min_row_var |
Minimum number of characters for the row variable. Default to 30. |
get_text |
Set to |
Value
A printed grouped table.
Printing method for class tabxplor_tab
Description
Printing method for class tabxplor_tab
Usage
## S3 method for class 'tabxplor_tab'
print(
x,
width = NULL,
...,
n = 100,
max_extra_cols = NULL,
max_footer_lines = NULL,
min_row_var = 30,
get_text = FALSE
)
Arguments
x |
Object to format or print. |
width |
Width of text output to generate. |
... |
Passed on to |
n |
Number of rows to show. |
max_extra_cols |
Number of extra columns to print abbreviated information for, if the width is too small for the entire tibble. |
max_footer_lines |
Maximum number of footer lines. |
min_row_var |
Minimum number of characters for the row variable. Default to 30. |
get_text |
Set to |
Value
A printed table.
relocate method for class tabxplor_grouped_tab
Description
relocate method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
relocate(.data, ...)
Arguments
.data |
A tibble of class |
... |
Columns to move. will move columns to the left-hand side; specifying both is an error. |
Value
An object of class tabxplor_grouped_tab
.
rename method for class tabxplor_grouped_tab
Description
rename method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
rename(.data, ...)
Arguments
.data |
A tibble of class |
... |
Use |
Value
An object of class tabxplor_grouped_tab
.
rename_with method for class tabxplor_grouped_tab
Description
rename_with method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
rename_with(.data, .fn, .cols = dplyr::everything(), ...)
Arguments
.data |
A tibble of class |
.fn |
A function used to transform the selected |
.cols |
Columns to rename; defaults to all columns. |
... |
Additional arguments passed onto |
Value
An object of class tabxplor_grouped_tab
.
rowwise method for class tabxplor_grouped_tab
Description
rowwise method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
rowwise(data, ...)
Arguments
data |
A tibble of class |
... |
Variables to be preserved when calling summarise(). This is typically a set of variables whose combination uniquely identify each row. |
Value
An object of class tabxplor_grouped_tab
and rowwise_df
.
rowwise method for class tabxplor_tab
Description
rowwise method for class tabxplor_tab
Usage
## S3 method for class 'tabxplor_tab'
rowwise(data, ...)
Arguments
data |
A tibble of class |
... |
Variables to be preserved
when calling |
Value
A tibble of class tabxplor_grouped_tab
and rowwise_df
.
Create a score variable from factors
Description
Create a score variable from factors
Usage
score_from_lv1(data, name, vars_list)
Arguments
data |
A data.frame. |
name |
The name of the variable to create. |
vars_list |
The list of the factors to count (only the first level is counted, as 1) ; as a character vector. |
Value
The data.frame, with a new variable.
Examples
data <- tibble::tibble(group = factor(c("G1", "G1", "G2", "G2", "G3", "G3")),
a = factor(c("Oui", "Oui", "Oui", "Oui", "Non", "Oui")),
b = factor(c("Oui", "Non", "Non", "Oui", "Non", "Oui")),
c = factor(c("Oui", "Oui", "Non", "Non", "Oui", "Oui")))
data |>
score_from_lv1("score", vars_list = c("a", "b", "c")) |>
tab(group, score, digits = 1)
select method for class tabxplor_grouped_tab
Description
select method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
select(.data, ...)
Arguments
.data |
A tibble of class |
... |
One or more unquoted expressions separated by commas. Variable names can be
used as if they were positions in the data frame, so expressions like |
Value
An object of class tabxplor_grouped_tab
.
Set the "display" vctrs::field of a fmt
vector.
Description
Set the "display" vctrs::field of a fmt
vector.
Usage
## S3 method for class 'data.frame'
set_display(x, value)
Arguments
x |
The object to test, to get a field in, or to modify. |
value |
The value you want to inject in some |
Value
The entered objects, with all fmt vectors with the wanted display.
Set the "display" vctrs::field of a fmt
vector.
Description
Set the "display" vctrs::field of a fmt
vector.
Usage
## Default S3 method:
set_display(x, value)
Arguments
x |
The object to test, to get a field in, or to modify. |
value |
The value you want to inject in some |
Value
The entered vector (nothing happens).
Set the "display" vctrs::field of a fmt
vector.
Description
Set the "display" vctrs::field of a fmt
vector.
Usage
## S3 method for class 'tabxplor_fmt'
set_display(x, value)
Arguments
x |
The object to test, to get a field in, or to modify. |
value |
The value you want to inject in some |
Value
A fmt vectors with the wanted display.
summarise method for class tabxplor_grouped_tab
Description
summarise method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
summarise(.data, ..., .groups = NULL)
Arguments
.data |
A tibble of class |
... |
Name-value pairs of summary functions. The name will be the name of the variable in the result. |
.groups |
Grouping structure of the result. |
Value
An object of class tabxplor_grouped_tab
.
Single cross-table, with color helpers
Description
A full-featured function to create, manipulate and format single
cross-tables, 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
,
like arrange
, filter
or mutate
.
Wrapper around the more powerful tab_many
.
Usage
tab(
data,
row_var,
col_var,
tab_vars,
wt,
sup_cols,
pct = "no",
color = "no",
OR = "no",
chi2 = FALSE,
na = "keep",
cleannames = NULL,
other_if_less_than = 0,
other_level = "Others",
ref = "auto",
ref2 = "first",
comp = "tab",
ci = "no",
conf_level = 0.95,
totaltab = "line",
totaltab_name = "Ensemble",
tot = c("row", "col"),
total_names = "Total",
add_n = TRUE,
add_pct = FALSE,
subtext = "",
digits = 0,
filter
)
Arguments
data |
A data frame. |
row_var , col_var |
The row variable, which will be printed with one level per line, and the column variable, which will be printed with one level per column. For numeric variables means are calculated, in a single column. |
tab_vars |
<tidy-select> Tab variables :
a subtable is made for each combination of levels of the selected variables.
Leave empty to make a simple cross-table. All |
wt |
A weight variable, of class numeric. Leave empty for unweighted results. |
sup_cols |
<tidy-select>
Supplementary columns variables, with only the first level printed, and row percentages
(for numeric variables, a mean will be calculated for each |
pct |
The type of percentages to calculate :
|
color |
The type of colors to print, as a single string :
|
OR |
With
|
chi2 |
Set to |
na |
The policy to adopt for missing values, as a single string :
|
cleannames |
Set to |
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.
|
ci |
The type of confidence intervals to calculate, passed to
By default, for percentages, with Wilson's method is used,
and with |
conf_level |
The confidence level, as a single numeric between 0 and 1. Default to 0.95 (95%). |
totaltab |
The total table, if there are subtables/groups
(i.e. when
|
totaltab_name |
The name of the total table, as a single string. |
tot |
The totals :
|
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 |
subtext |
A character vector to print rows of legend under the table. |
digits |
The number of digits to print, as a single integer. To print a different
number of digits for each |
filter |
A |
Value
A tibble
of class tab
, possibly with colored reading helpers.
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
.
Examples
# A simple cross-table:
tab(forcats::gss_cat, marital, race)
# With more variables provided, `tab` makes a subtables for each combination of levels:
tab(forcats::gss_cat, marital, tab_vars = c(year, race))
# You can also add supplementary columns, text or numeric:
tab(dplyr::storms, category, status, sup_cols = c("pressure", "wind"))
# Colors to help the user read the table:
data <- forcats::gss_cat %>%
dplyr::filter(year %in% c(2000, 2006, 2012), !marital %in% c("No answer", "Widowed"))
gss <- "Source: General social survey 2000-2014"
gss2 <- "Source: General social survey 2000, 2006 and 2012"
# Differences between the cell and it's subtable's total cell:
tab(data, race, marital, year, subtext = gss2, pct = "row", color = "diff")
# Differences between the cell and the whole table's general total cell:
tab(data, race, marital, year, subtext = gss2, pct = "row", color = "diff",
comp = "all")
# Historical differences:
data2 <- data %>% dplyr::mutate(year = as.factor(year))
tab(data2, year, marital, race, subtext = gss2, pct = "row",
color = "diff", ref = "first", tot = "col")
# Differences with the total, except if their confidences intervals are superior to them:
tab(forcats::gss_cat, race, marital, subtext = gss, pct = "row", color = "diff_ci")
# Same differences, minus their confidence intervals:
tab(forcats::gss_cat, race, marital, subtext = gss, pct = "row", color = "after_ci")
# Contribution of cells to table's variance, like in a correspondence analysis:
tab(forcats::gss_cat, race, marital, subtext = gss, color = "contrib")
# Since the result is a tibble, you can use all dplyr verbs to modify it :
library(dplyr)
tab(dplyr::storms, category, status, sup_cols = c("pressure", "wind")) %>%
dplyr::filter(category != "-1") %>%
dplyr::select(-`tropical depression`) %>%
dplyr::arrange(is_totrow(.), desc(category))
# With `dplyr::arrange`, don't forget to keep the order of tab variables and total rows:
tab(data, race, marital, year, pct = "row") %>%
dplyr::arrange(year, is_totrow(.), desc(Married))
Coercion between two tab
Description
Coercion between two tab
Usage
tab_cast(x, to, ..., x_arg = "", to_arg = "")
tab_ptype2(x, y, ..., x_arg = "", y_arg = "")
## S3 method for class 'tabxplor_tab.tabxplor_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_tab.tabxplor_tab'
vec_cast(x, to, ...)
## S3 method for class 'tabxplor_tab.tbl_df'
vec_ptype2(x, y, ...)
## S3 method for class 'tbl_df.tabxplor_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_tab.tbl_df'
vec_cast(x, to, ...)
## S3 method for class 'tbl_df.tabxplor_tab'
vec_cast(x, to, ...)
## S3 method for class 'tabxplor_tab.data.frame'
vec_ptype2(x, y, ...)
## S3 method for class 'data.frame.tabxplor_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_tab.data.frame'
vec_cast(x, to, ...)
## S3 method for class 'data.frame.tabxplor_tab'
vec_cast(x, to, ...)
gtab_cast(x, to, ..., x_arg = "", to_arg = "")
gtab_ptype2(x, y, ..., x_arg = "", y_arg = "")
## S3 method for class 'tabxplor_grouped_tab.tabxplor_grouped_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_grouped_tab.tabxplor_grouped_tab'
vec_cast(x, to, ...)
## S3 method for class 'tabxplor_grouped_tab.grouped_df'
vec_ptype2(x, y, ...)
## S3 method for class 'grouped_df.tabxplor_grouped_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_grouped_tab.grouped_df'
vec_cast(x, to, ...)
## S3 method for class 'grouped_df.tabxplor_grouped_tab'
vec_cast(x, to, ...)
## S3 method for class 'tabxplor_grouped_tab.tabxplor_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_tab.tabxplor_grouped_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_grouped_tab.tabxplor_tab'
vec_cast(x, to, ...)
## S3 method for class 'tabxplor_tab.tabxplor_grouped_tab'
vec_cast(x, to, ...)
## S3 method for class 'tabxplor_grouped_tab.tbl_df'
vec_ptype2(x, y, ...)
## S3 method for class 'tbl_df.tabxplor_grouped_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_grouped_tab.tbl_df'
vec_cast(x, to, ...)
## S3 method for class 'tbl_df.tabxplor_grouped_tab'
vec_cast(x, to, ...)
## S3 method for class 'tabxplor_grouped_tab.data.frame'
vec_ptype2(x, y, ...)
## S3 method for class 'data.frame.tabxplor_grouped_tab'
vec_ptype2(x, y, ...)
## S3 method for class 'tabxplor_grouped_tab.data.frame'
vec_cast(x, to, ...)
## S3 method for class 'data.frame.tabxplor_grouped_tab'
vec_cast(x, to, ...)
Arguments
x , y , to |
Subclasses of data frame. |
... |
For future extensions. |
x_arg |
Argument names for x and y. These are used in error messages to inform the user about the locations of incompatible types. |
to_arg |
Argument names for x and to. These are used in error messages to inform the user about the locations of incompatible types. |
y_arg |
Argument names for x and y. These are used in error messages to inform the user about the locations of incompatible types. |
Value
A tibble of class tabxplor_tab
.
A tibble of class tabxplor_tab
.
A tibble of class tabxplor_tab
.
A tibble of class tabxplor_tab
.
A tibble of class tabxplor_tab
.
A tibble.
A tibble of class tabxplor_tab
.
A tibble.
A tibble of class tabxplor_tab
.
A data.frame.
A tibble of class tabxplor_tab
.
A data.frame.
An object of class tabxplor_grouped_tab
.
An object of class tabxplor_grouped_tab
.
An object of class tabxplor_grouped_tab
.
An object of class grouped_df
.
An object of class tabxplor_grouped_tab
.
An object of class grouped_df
.
An object of class tabxplor_grouped_tab
.
An object of class tabxplor_tab
.
An object of class tabxplor_grouped_tab
.
An object of class tabxplor_tab
.
An object of class tabxplor_grouped_tab
.
An object of class tbl_df
.
An object of class tabxplor_grouped_tab
.
An object of class tbl_df
.
An object of class tabxplor_grouped_tab
.
An data.frame.
An object of class tabxplor_grouped_tab
.
An data.frame.
Functions
-
vec_ptype2(tabxplor_tab.tabxplor_tab)
: find common ptype between tabxplor_tab and tabxplor_tab -
vec_cast(tabxplor_tab.tabxplor_tab)
: convert tabxplor_tab to tabxplor_tab -
vec_ptype2(tabxplor_tab.tbl_df)
: find common ptype between tabxplor_tab and tbl_df -
vec_ptype2(tbl_df.tabxplor_tab)
: find common ptype between tbl_df and tabxplor_tab -
vec_cast(tabxplor_tab.tbl_df)
: convert tbl_df to tabxplor_tab -
vec_cast(tbl_df.tabxplor_tab)
: convert tabxplor_tab to tbl_df -
vec_ptype2(tabxplor_tab.data.frame)
: find common ptype between tabxplor_tab and data.frame -
vec_ptype2(data.frame.tabxplor_tab)
: find common ptype between data.frame and tabxplor_tab -
vec_cast(tabxplor_tab.data.frame)
: convert data.frame to tabxplor_tab -
vec_cast(data.frame.tabxplor_tab)
: convert tabxplor_tab to data.frame -
vec_ptype2(tabxplor_grouped_tab.tabxplor_grouped_tab)
: find common ptype between tabxplor_grouped_tab and tabxplor_grouped_tab -
vec_cast(tabxplor_grouped_tab.tabxplor_grouped_tab)
: convert tabxplor_grouped_tab to tabxplor_grouped_tab -
vec_ptype2(tabxplor_grouped_tab.grouped_df)
: find common ptype between tabxplor_grouped_tab and grouped_df -
vec_ptype2(grouped_df.tabxplor_grouped_tab)
: find common ptype between grouped_df and tabxplor_grouped_tab -
vec_cast(tabxplor_grouped_tab.grouped_df)
: convert grouped_df to tabxplor_grouped_tab -
vec_cast(grouped_df.tabxplor_grouped_tab)
: convert tabxplor_grouped_tab to grouped_df -
vec_ptype2(tabxplor_grouped_tab.tabxplor_tab)
: find common ptype between tabxplor_grouped_tab and tabxplor_tab -
vec_ptype2(tabxplor_tab.tabxplor_grouped_tab)
: find common ptype between tabxplor_tab and tabxplor_grouped_tab -
vec_cast(tabxplor_grouped_tab.tabxplor_tab)
: convert tabxplor_tab to tabxplor_grouped_tab -
vec_cast(tabxplor_tab.tabxplor_grouped_tab)
: convert tabxplor_grouped_tab to tabxplor_tab -
vec_ptype2(tabxplor_grouped_tab.tbl_df)
: find common ptype between tabxplor_grouped_tab and tbl_df -
vec_ptype2(tbl_df.tabxplor_grouped_tab)
: find common ptype between tbl_df and tabxplor_grouped_tab -
vec_cast(tabxplor_grouped_tab.tbl_df)
: convert tbl_df to tabxplor_grouped_tab -
vec_cast(tbl_df.tabxplor_grouped_tab)
: convert tabxplor_grouped_tab to tbl_df -
vec_ptype2(tabxplor_grouped_tab.data.frame)
: find common ptype between tabxplor_grouped_tab and data.frame -
vec_ptype2(data.frame.tabxplor_grouped_tab)
: find common ptype between data.frame and tabxplor_grouped_tab -
vec_cast(tabxplor_grouped_tab.data.frame)
: convert data.frame to tabxplor_grouped_tab -
vec_cast(data.frame.tabxplor_grouped_tab)
: convert tabxplor_grouped_tab to data.frame
Add Chi2 summaries to a tab
Description
Add Chi2 summaries to a tab
Usage
tab_chi2(
tabs,
calc = c("ctr", "p", "var", "counts"),
comp = NULL,
color = c("no", "auto", "all", "all_pct")
)
Arguments
tabs |
|
calc |
By default all elements of the Chi2 summary are calculated :
contributions to variance, pvalue, variance and unweighted count. You can choose which
are computed by selecting elements in the vector |
comp |
Comparison level. When |
color |
The type of colors to print, as a single string.
|
Value
A tibble
of class tab
, with Chi2 summaries as metadata,
possibly colored based on contributions of cells to variance.
Add confidence intervals to a tab
Description
Add confidence intervals to a tab
Usage
tab_ci(
tabs,
ci = "auto",
comp = NULL,
conf_level = 0.95,
color = "no",
visible = FALSE,
method_cell = "wilson",
method_diff = "ac"
)
Arguments
tabs |
|
ci |
The type of ci to calculate. Set to "cell" to calculate absolute confidence
intervals. Set to "diff" to calculate the confidence intervals of the difference
between a cell and the relative total cell (or the reference cell,
when |
comp |
Comparison level. When |
conf_level |
The confidence level, as a single numeric between 0 and 1. Default to 0.95 (95%). |
color |
The type of colors to print, as a single string.
|
visible |
By default confidence intervals are calculated and used to set colors,
but not printed. Set to |
method_cell |
Character string specifying which method to use with percentages
for |
method_diff |
Character string specifying which method to use with percentages
for |
Value
A tibble
of class tab
, colored based on differences (from
totals/first cells) and confidence intervals.
Examples
# A typical workflow with tabxplor step-by-step functions :
data <- dplyr::starwars %>%
tab_prepare(sex, hair_color, gender, other_if_less_than = 5,
na_drop_all = sex)
data %>%
tab_plain(sex, hair_color, gender, tot = c("row", "col"),
pct = "row", comp = "all") %>%
tab_ci("diff", color = "after_ci")
Bind a list of tabs with the same col_vars (and no tab_vars) into a single tab
Description
Bind a list of tabs with the same col_vars (and no tab_vars) into a single tab
Usage
tab_compact(tabs)
Arguments
tabs |
A |
Value
A tabxplor_tab
Examples
forcats::gss_cat |>
tab_many(c(race, rincome), marital, pct = "row", color = "diff") |>
tab_compact()
Get the number of actual rows and the max character length of a table after
being wrapped (count \n
as a linebreak).
Description
Get the number of actual rows and the max character length of a table after
being wrapped (count \n
as a linebreak).
Usage
tab_get_wrapped_dimensions(tabs, no_tab_vars = FALSE, width_pad = 4L)
Arguments
tabs |
A data.frame. |
no_tab_vars |
For data.frame of class |
width_pad |
Number of characters lengths between columns. |
Print a tabxplor table in html
Description
Print a tabxplor table in html
Usage
tab_kable(
tabs,
theme = c("light", "dark"),
color_type = NULL,
html_24_bit = NULL,
tooltips = TRUE,
popover = NULL,
color_legend = TRUE,
caption = knitr::opts_current$get("tab.cap"),
html_font = NULL,
get_data = FALSE,
full_width = FALSE,
wrap_rows = 35,
wrap_cols = 15,
whitespace_only = TRUE,
...
)
Arguments
tabs |
A table made with |
theme |
By default, a white table with black text, Set to |
color_type |
Set to |
html_24_bit |
Use 24bits colors palettes for html tables : set to |
tooltips |
By default, html tooltips are used to display additional informations
at mouse hover. Set to |
popover |
By default, takes |
color_legend |
Print colors legend below the table ?
You can then use a |
caption |
The table caption. For formatting, you need to use a |
html_font |
A string for HTML css font. By default, it uses
|
get_data |
Get the transformed data instead of the html table. |
full_width |
A TRUE or FALSE variable controlling whether the HTML table should have the preferable format for full_width. If not specified, a HTML table will have full width by default but this option will be set to FALSE for a LaTeX table. |
wrap_rows |
By default, rownames are wrapped when larger than 30 characters. |
wrap_cols |
By default, colnames are wrapped when larger than 12 characters. |
whitespace_only |
Set to |
... |
Other arguments to pass to |
Value
A html table (opened in the viewer in RStudio). Differences from totals, confidence intervals, contribution to variance, and unweighted counts, are available in an html tooltip at cells hover.
Examples
tabs <- tab(forcats::gss_cat, race, marital, year, pct = "row", color = "diff")
tab_kable(tabs, theme = "light", color_type = "text")
Many cross-tables as one, with color helpers
Description
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.
Usage
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"))
Arguments
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 |
Value
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.
Functions
-
tab_get_vars()
: Get the variables names of a tabxplortab
-
is_tab()
: a test function for class tabxplor_tab -
set_color_style()
: define the color style used to printtab
. -
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
Examples
# 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)
)
Means table
Description
Cross categorical variables with numeric variables, and get a table of means and standard deviations.
Usage
tab_num(
data,
row_var,
col_vars,
tab_vars,
wt,
color = c("auto", "diff", "diff_ci", "after_ci"),
na = c("keep", "drop", "drop_fct", "drop_num"),
ref = "tot",
comp = c("tab", "all"),
ci = NULL,
conf_level = 0.95,
totaltab = "line",
totaltab_name = "Ensemble",
tot = NULL,
total_names = "Total",
subtext = "",
digits = 0,
num = FALSE,
df = FALSE
)
Arguments
data |
A data frame. |
row_var |
The row variable, which will be printed with one level per line. If numeric, it will be used as a factor. |
col_vars |
The numeric variables, which will appear in columns :
means and standard deviation are calculated for each levels of |
tab_vars |
<tidy-select> Tab variables : a subtable is made for each combination of levels of the selected variables. Leave empty to make a simple cross-table. All tab variables are converted to factor. |
wt |
A weight variable, of class numeric. Leave empty for unweighted results. |
color |
|
na |
The policy to adopt for missing values in row and tab variables (factors), as a single string.
|
ref |
The reference cell to calculate differences and ratios
(used to print
|
comp |
Comparison level. When |
ci |
The type of confidence intervals to calculate, passed to
|
conf_level |
The confidence level for the confidence intervals, as a single numeric between 0 and 1. Default to 0.95 (95%). |
totaltab |
The total table,
if there are subtables/groups (i.e. when
|
totaltab_name |
The name of the total table, as a single string. |
tot |
The totals :
|
total_names |
The names of the totals, as a character vector of length one or two.
Use syntax of type |
subtext |
A character vector to print rows of legend under the table. |
digits |
The number of digits to print, as a single integer. |
num |
Set to |
df |
Set to |
Value
A tibble
of class tabxplor_tab
. If ...
(tab_vars
)
are provided, a tab
of class tabxplor_grouped_tab
.
All non-text columns are fmt
vectors of class tabxplor_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
.
Examples
data <- dplyr::storms %>% tab_prepare(category, wind, na_drop_all = wind)
tab_num(data, category, wind, tot = "row", color = "after_ci")
Add percentages and diffs to a tab
Description
Add percentages and diffs to a tab
Usage
tab_pct(
tabs,
pct = "row",
digits = NULL,
ref = c("tot", "first", "no"),
comp = NULL,
color = FALSE,
just_diff = FALSE
)
Arguments
tabs |
|
pct |
The type of percentages to calculate. |
digits |
The number of digits to print for percentages. As a single integer,
or an integer vector the same length than |
ref |
The reference cell to calculate differences and ratios
(used to print
|
comp |
Comparison level. When |
color |
Set to |
just_diff |
If percentages are already calculated and you just want to recalculate differences. |
Value
A tibble
of class tab
, with percentages displayed, possibly
colored based on differences from totals or first cell.
Plain single cross-table
Description
Plain single cross-table
Usage
tab_plain(
data,
row_var,
col_var,
tab_vars,
wt,
pct = "no",
color = "no",
OR = "no",
na = "keep",
ref = "auto",
ref2 = "first",
comp = "tab",
totaltab = "line",
totaltab_name = "Ensemble",
tot = NULL,
total_names = "Total",
subtext = "",
digits = 0,
num = FALSE,
df = FALSE
)
Arguments
data |
A data frame. |
row_var , col_var |
The row variable, which will be printed with one level per line,
and the column variable, which will be printed with one level per column. Numeric
variables will be used as factors. To calculate means, use |
tab_vars |
<tidy-select> Tab variables : a subtable is made for each combination of levels of the selected variables. Leave empty to make a simple cross-table. All tab variables are converted to factor. |
wt |
A weight variable, of class numeric. Leave empty for unweighted results. |
pct |
The type of percentages to calculate :
|
color |
The type of colors to print, as a single string :
|
OR |
With
|
na |
The policy to adopt with missing values, 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 |
Comparison level. When |
totaltab |
The total table,
if there are subtables/groups (i.e. when
|
totaltab_name |
The name of the total table, as a single string. |
tot |
The totals :
|
total_names |
The names of the totals, as a character vector of length one or two.
Use syntax of type |
subtext |
A character vector to print rows of legend under the table. |
digits |
The number of digits to print, as a single integer. |
num |
Set to |
df |
Set to |
Value
A tibble
of class tabxplor_tab
. If ...
(tab_vars
)
are provided, a tab
of class tabxplor_grouped_tab
.
All non-text columns are fmt
vectors of class tabxplor_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
.
Examples
# A typical workflow with tabxplor step-by-step functions :
data <- dplyr::starwars %>% tab_prepare(sex, hair_color)
data %>%
tab_plain(sex, hair_color, tot = c("row", "col"), pct = "row") %>%
tab_chi2() %>%
tab_ci(color = "after_ci")
Print a tabxplor table as plot
Description
Print a tabxplor table as plot
Usage
tab_plot(
tabs,
theme = c("light", "dark"),
color_type = NULL,
html_24_bit = NULL,
color_legend = TRUE,
caption = NULL,
wrap_rows = 35,
wrap_cols = 14,
whitespace_only = TRUE
)
Arguments
tabs |
|
theme |
By default, a white table with black text, Set to |
color_type |
Set to |
html_24_bit |
Use 24bits colors palettes for html tables : set to |
color_legend |
Print colors legend below the table ? |
caption |
The table caption. |
wrap_rows |
By default, rownames are wrapped when larger than 30 characters. |
wrap_cols |
By default, colnames are wrapped when larger than 12 characters. |
whitespace_only |
Set to |
Value
A ggplot
object to be printed in the
RStudio
Plots pane or exported as image, using ggtexttable
.
Examples
tab(forcats::gss_cat, race, marital, pct = "row", color = "diff") |>
tab_plot()
Prepare data for tab_plain
.
Description
Prepare data for tab_plain
.
Usage
tab_prepare(
data,
...,
na_drop_all,
cleannames = NULL,
other_if_less_than = 0,
other_level = "Others"
)
Arguments
data |
A dataframe. |
... |
Variables then to be passed in |
na_drop_all |
<tidy-select> Removes all
observation with a |
cleannames |
Set to |
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 character vector of length one. |
Value
A modified data.frame.
Examples
data <- dplyr::starwars %>%
tab_prepare(sex, hair_color, gender, other_if_less_than = 5,
na_drop_all = sex)
data
Transform chi2 attribute table of a tabxplor_tab into rows with pvalues.
Description
Transform chi2 attribute table of a tabxplor_tab into rows with pvalues.
Usage
tab_pvalue_lines(tabs)
Arguments
tabs |
A tabxplor_tab (with chi2 table as attribute). |
Value
A tabxplor_tab.
Spread a tab, passing a tab variable to column
Description
Spread a tab, passing a tab variable to column
Usage
tab_spread(
tabs,
spread_vars,
names_prefix,
names_sort = FALSE,
totname = "Total"
)
Arguments
tabs |
A |
spread_vars |
<tidy-select> The tab variables
to pass to column, with a syntax of type |
names_prefix |
String added to the start of every variable name. |
names_sort |
If no |
totname |
The new name of the total rows, as a single string. |
Value
A tibble
of class tab
, with less rows and more columns.
Examples
data <- forcats::gss_cat %>% dplyr::filter(year %in% c(2000, 2014))
tabs <-
tab(data, relig, marital, c(year, race), pct = "row", totaltab = "no",
color = "diff", tot = "row", other_if_less_than = 30)
tabs %>%
dplyr::select(year, race, relig, Married) %>%
tab_spread(race)
Add totals to a tab
Description
Add totals to a tab
Usage
tab_tot(
tabs,
tot = c("row", "col"),
name = "Total",
totcol = "last",
data = NULL
)
Arguments
tabs |
|
tot |
|
name |
The names of the totals, as a character vector of length one or two.
Use |
totcol |
|
data |
The original database used to calculate the |
Value
A tibble
of class tab
. Total rows can then be detected using
is_totrow
, and total columns using is_totcol
.
Examples
data <- dplyr::starwars %>% tab_prepare(sex, hair_color)
data %>%
tab_plain(sex, hair_color) %>%
tab_tot("col", totcol = "each")
Add total table to a tab
Description
Add total table to a tab
Usage
tab_totaltab(
tabs,
totaltab = c("table", "line", "no"),
name = "Ensemble",
data = NULL
)
Arguments
tabs |
|
totaltab |
If there are subtables, corresponding to the levels of tab_vars,
|
name |
The name of the total table, as a single string. |
data |
The original database used to calculate the |
Value
A tibble
of class tab
. Rows belonging to the total table can then
be detected using is_tottab
.
Examples
data <- dplyr::starwars %>%
tab_prepare(sex, hair_color, gender, other_if_less_than = 5,
na_drop_all = sex)
data %>%
tab_plain(sex, hair_color, gender) %>%
tab_totaltab("line")
Wrap column names and character/factor variables.
Description
Wrap column names and character/factor variables.
Usage
tab_wrap_text(
tabs,
wrap_rows = 35L,
wrap_cols = 15L,
exdent = 1,
whitespace_only = TRUE,
unbreakable_spaces = TRUE,
brk = "\n"
)
Arguments
tabs |
A |
wrap_rows |
By default, rownames are wrapped when larger than 30 characters. |
wrap_cols |
By default, colnames are wrapped when larger than 12 characters. |
exdent |
On the second lines or more, the number or characters to use for indentation. |
whitespace_only |
Set to |
unbreakable_spaces |
Set to |
brk |
The string to use for linebreak : |
Value
The same tabxplor_tab
or tibble
.
Examples
tab(forcats::gss_cat, race, marital, pct = "row", color = "diff") |>
tab_wrap_text(wrap_rows = 5L, wrap_cols = 8L)
Excel output for tabxplor tables, with formatting and colors
Description
To modify the colors used into the Excel table, you can change the
global options with set_color_style
and set_color_breaks
.
Usage
tab_xl(
tabs,
path = NULL,
replace = FALSE,
open = rlang::is_interactive(),
colnames_rotation = 0,
remove_tab_vars = TRUE,
colwidth = 10,
print_color_legend = TRUE,
sheets = "auto",
n_min = 0,
titles,
font_text = "DejaVu Sans Condensed",
font_num = "DejaVu Sans",
text_size = 10,
text_size_headers = 9,
text_size_subtext = 9,
hide_near_zero = Inf,
color_type = "text"
)
Arguments
tabs |
A table made with |
path , replace , open |
The name, and possibly the path, of the Excel file to
create (possibly without the .xlsx extension). Default path to temporary directory.
Set global option |
colnames_rotation |
Rotate the names of columns to an angle (in degrees). |
remove_tab_vars |
By default, |
colwidth |
The standard width for numeric columns, as a number.
Set to |
print_color_legend |
Should the color legends be printed with the subtexts ? |
sheets |
The Excel sheets options :
|
n_min |
The total count under which a column or row is turned pale grey because there is not enough observation for it to be significant. Default to 0 (not used). |
titles |
The titles of the different tables, as a character vector. When missing titles are given based on the names of the variables. |
font_text , font_num |
Font for text and for numbers. |
text_size , text_size_headers , text_size_subtext |
Font sizes of text elements. |
hide_near_zero |
By default all cells displayed as 0 (even rounded)
turn pale grey, to make the distribution of empty cells (and other cells) more visible.
Provide a number to turn grey every cell below it. Set to |
color_type |
By default, the text is colored. Set to |
Value
The table(s) with formatting and colors in an Excel file, as a side effect.
Invisibly returns tabs
.
Examples
forcats::gss_cat %>%
tab(marital, race, pct = "row", color = "diff") %>%
tab_xl()
Internal data.table methods
Description
Internal data.table methods
Internal vctrs methods
Description
Internal vctrs methods
Table body for class tab
Description
Table body for class tab
Usage
## S3 method for class 'tabxplor_tab'
tbl_format_body(x, setup, ...)
Arguments
x |
An object of class tabxplor_tab |
setup |
A setup object from the table |
... |
Other parameters. |
Value
A character vector.
Table footer for class tab
Description
Table footer for class tab
Usage
## S3 method for class 'tabxplor_tab'
tbl_format_footer(x, setup, ...)
Arguments
x |
An object of class tabxplor_tab |
setup |
A setup object from the table |
... |
Other parameters. |
Value
A character vector.
Table headers for class grouped tab
Description
Table headers for class grouped tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
tbl_sum(x, ...)
Arguments
x |
An object of class tabxplor_tab |
... |
Other parameters. |
Value
A table header
Table headers for class tab
Description
Table headers for class tab
Usage
## S3 method for class 'tabxplor_tab'
tbl_sum(x, ...)
Arguments
x |
An object of class tabxplor_tab |
... |
Other parameters. |
Value
A table header
ungroup method for class tabxplor_grouped_tab
Description
ungroup method for class tabxplor_grouped_tab
Usage
## S3 method for class 'tabxplor_grouped_tab'
ungroup(x, ...)
Arguments
x |
A tibble of class |
... |
Variables to remove from the grouping. |
Value
An object of class tabxplor_tab
or tabxplor_grouped_tab
.
Vec_arith method for fmt
Description
Vec_arith method for fmt
Usage
## S3 method for class 'tabxplor_fmt'
vec_arith(op, x, y, ...)
## Default S3 method:
vec_arith.tabxplor_fmt(op, x, y, ...)
## S3 method for class 'tabxplor_fmt'
vec_arith.tabxplor_fmt(op, x, y, ...)
## S3 method for class 'numeric'
vec_arith.tabxplor_fmt(op, x, y, ...)
## S3 method for class 'tabxplor_fmt'
vec_arith.numeric(op, x, y, ...)
## S3 method for class 'MISSING'
vec_arith.tabxplor_fmt(op, x, y, ...)
Arguments
op |
Operation to do. |
x |
fmt object. |
y |
Second object. |
... |
Other parameter. |
Value
A fmt vector
A fmt vector
A fmt vector
A fmt vector
A fmt vector
A fmt vector
Methods (by class)
-
vec_arith.tabxplor_fmt(default)
: default vec_arith method for fmt -
vec_arith.tabxplor_fmt(tabxplor_fmt)
: vec_arith method for fmt + fmt -
vec_arith.tabxplor_fmt(numeric)
: vec_arith method for fmt + numeric -
vec_arith.tabxplor_fmt(MISSING)
: vec_arith method for -fmt
Functions
-
vec_arith.numeric(tabxplor_fmt)
: vec_arith method for numeric + fmt
Convert fmt into character
Description
Convert fmt into character
Usage
## S3 method for class 'tabxplor_fmt'
vec_cast.character(x, to, ...)
Arguments
x |
A fmt vector |
to |
A character vector |
... |
Other parameter |
Value
A character vector
Convert fmt into double
Description
Convert fmt into double
Usage
## S3 method for class 'tabxplor_fmt'
vec_cast.double(x, to, ...)
Arguments
x |
A fmt vector |
to |
A double vector |
... |
Other parameter. |
Value
A double vector
Convert fmt into integer
Description
Convert fmt into integer
Usage
## S3 method for class 'tabxplor_fmt'
vec_cast.integer(x, to, ...)
Arguments
x |
A integer vector |
to |
A fmt vector |
... |
Other parameter. |
Value
An integer vector
Convert double into fmt
Description
Convert double into fmt
Usage
## S3 method for class 'tabxplor_fmt.double'
vec_cast(x, to, ...)
Arguments
x |
A double vector |
to |
A fmt vector |
... |
Other parameter. |
Value
A fmt vector
Convert integer into fmt
Description
Convert integer into fmt
Usage
## S3 method for class 'tabxplor_fmt.integer'
vec_cast(x, to, ...)
Arguments
x |
A integer vector |
to |
A fmt vector |
... |
Other parameter. |
Value
A fmt vector
Convert fmt into fmt
Description
Convert fmt into fmt
Usage
## S3 method for class 'tabxplor_fmt.tabxplor_fmt'
vec_cast(x, to, ...)
Arguments
x |
A fmt vector |
to |
A fmt vector |
... |
Other parameter. |
Value
A fmt vector
Vec_math method for class fmt
Description
Vec_math method for class fmt
Usage
## S3 method for class 'tabxplor_fmt'
vec_math(.fn, .x, ...)
Arguments
.fn |
A function |
.x |
A fmt object |
... |
Other parameter |
Value
A fmt vector
Compare with fmt vector
Description
Compare with fmt vector
Usage
## S3 method for class 'tabxplor_fmt'
vec_proxy_compare(x, ...)
Arguments
x |
A fmt vector |
... |
Other parameter |
Value
A double vector
Test equality with fmt vector
Description
Test equality with fmt vector
Usage
## S3 method for class 'tabxplor_fmt'
vec_proxy_equal(x, ...)
Arguments
x |
A fmt vector |
... |
Other parameter |
Value
A double vector
Find common ptype between double and fmt
Description
Find common ptype between double and fmt
Usage
## S3 method for class 'double.tabxplor_fmt'
vec_ptype2(x, y, ...)
Arguments
x |
A double vector |
y |
A fmt vector |
... |
Other parameter. |
Value
A fmt vector
Find common ptype between integer and fmt
Description
Find common ptype between integer and fmt
Usage
## S3 method for class 'integer.tabxplor_fmt'
vec_ptype2(x, y, ...)
Arguments
x |
An integer vector |
y |
A fmt vector |
... |
Other parameter. |
Value
A fmt vector
Find common ptype between fmt and double
Description
Find common ptype between fmt and double
Usage
## S3 method for class 'tabxplor_fmt.double'
vec_ptype2(x, y, ...)
Arguments
x |
A fmt vector |
y |
A double vector |
... |
Other parameter. |
Value
A fmt vector
Find common ptype between fmt and integer
Description
Find common ptype between fmt and integer
Usage
## S3 method for class 'tabxplor_fmt.integer'
vec_ptype2(x, y, ...)
Arguments
x |
A fmt vector |
y |
An integer vector |
... |
Other parameter. |
Value
A fmt vector
Find common ptype between fmt and fmt
Description
Find common ptype between fmt and fmt
Usage
## S3 method for class 'tabxplor_fmt.tabxplor_fmt'
vec_ptype2(x, y, ...)
Arguments
x |
A fmt object. |
y |
A fmt object. |
... |
Other parameter. |
Value
A fmt vector
Abbreviated display name for class fmt in tibbles
Description
Abbreviated display name for class fmt in tibbles
Usage
## S3 method for class 'tabxplor_fmt'
vec_ptype_abbr(x, ...)
Arguments
x |
A fmt object. |
... |
Other parameter. |
Value
A single string with abbreviated fmt type.
Printed type for class fmt
Description
Printed type for class fmt
Usage
## S3 method for class 'tabxplor_fmt'
vec_ptype_full(x, ...)
Arguments
x |
A fmt object. |
... |
Other parameter. |
Value
A single string with full fmt type.