Generates a simple cross-table of two variables from a dataset with custom options for showing percentages and sub-totals.
Usage
tm_t_crosstable(
label = "Cross Table",
x,
y,
show_percentage = TRUE,
show_total = TRUE,
pre_output = NULL,
post_output = NULL,
basic_table_args = teal.widgets::basic_table_args(),
decorators = NULL
)
Arguments
- label
(
character(1)
) Label shown in the navigation item for the module or module group. Formodules()
defaults to"root"
. SeeDetails
.- x
(
data_extract_spec
orlist
of multipledata_extract_spec
) Object with all available choices with pre-selected option for variable X - row values. In case ofdata_extract_spec
useselect_spec(..., ordered = TRUE)
if table elements should be rendered according to selection order.- y
-
(
data_extract_spec
orlist
of multipledata_extract_spec
) Object with all available choices with pre-selected option for variable Y - column values.data_extract_spec
must not allow multiple selection in this case. - show_percentage
(
logical(1)
) Indicates whether to show percentages (relevant only whenx
is afactor
). Defaults toTRUE
.- show_total
(
logical(1)
) Indicates whether to show total column. Defaults toTRUE
.- pre_output
(
shiny.tag
) optional, text or UI element to be displayed before the module's output, providing context or a title. with text placed before the output to put the output into context. For example a title.- post_output
(
shiny.tag
) optional, text or UI element to be displayed after the module's output, adding context or further instructions. Elements likeshiny::helpText()
are useful.- basic_table_args
-
(
basic_table_args
) object created byteal.widgets::basic_table_args()
with settings for the module table. The argument is merged with options variableteal.basic_table_args
and default module setup.For more details see the vignette:
vignette("custom-basic-table-arguments", package = "teal.widgets")
- decorators
-
(
list
ofteal_transform_module
, namedlist
ofteal_transform_module
orNULL
) optional, if notNULL
, decorator for tables or plots included in the module. When a named list ofteal_transform_module
, the decorators are applied to the respective output objects.Otherwise, the decorators are applied to all objects, which is equivalent as using the name
default
.See section "Decorating
tm_t_crosstable
" below for more details.
Note
For more examples, please see the vignette "Using cross table" via
vignette("using-cross-table", package = "teal.modules.general")
.
Decorating tm_t_crosstable
This module generates the following objects, which can be modified in place using decorators:
table
(ElementaryTable
- output ofrtables::build_table
)
For additional details and examples of decorators, refer to the vignette
vignette("decorate-modules-output", package = "teal")
or the teal::teal_transform_module()
documentation.
Examples
# general data example
data <- teal_data()
data <- within(data, {
mtcars <- mtcars
for (v in c("cyl", "vs", "am", "gear")) {
mtcars[[v]] <- as.factor(mtcars[[v]])
}
mtcars[["primary_key"]] <- seq_len(nrow(mtcars))
})
join_keys(data) <- join_keys(join_key("mtcars", "mtcars", "primary_key"))
app <- init(
data = data,
modules = modules(
tm_t_crosstable(
label = "Cross Table",
x = data_extract_spec(
dataname = "mtcars",
select = select_spec(
label = "Select variable:",
choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
selected = c("cyl", "gear"),
multiple = TRUE,
ordered = TRUE,
fixed = FALSE
)
),
y = data_extract_spec(
dataname = "mtcars",
select = select_spec(
label = "Select variable:",
choices = variable_choices(data[["mtcars"]], c("cyl", "vs", "am", "gear")),
selected = "vs",
multiple = FALSE,
fixed = FALSE
)
)
)
)
)
#> Initializing tm_t_crosstable
#> Initializing reporter_previewer_module
if (interactive()) {
shinyApp(app$ui, app$server)
}
# CDISC data example
data <- teal_data()
data <- within(data, {
ADSL <- teal.data::rADSL
})
join_keys(data) <- default_cdisc_join_keys[names(data)]
app <- init(
data = data,
modules = modules(
tm_t_crosstable(
label = "Cross Table",
x = data_extract_spec(
dataname = "ADSL",
select = select_spec(
label = "Select variable:",
choices = variable_choices(data[["ADSL"]], subset = function(data) {
idx <- !vapply(data, inherits, logical(1), c("Date", "POSIXct", "POSIXlt"))
return(names(data)[idx])
}),
selected = "COUNTRY",
multiple = TRUE,
ordered = TRUE,
fixed = FALSE
)
),
y = data_extract_spec(
dataname = "ADSL",
select = select_spec(
label = "Select variable:",
choices = variable_choices(data[["ADSL"]], subset = function(data) {
idx <- vapply(data, is.factor, logical(1))
return(names(data)[idx])
}),
selected = "SEX",
multiple = FALSE,
fixed = FALSE
)
)
)
)
)
#> Initializing tm_t_crosstable
#> Initializing reporter_previewer_module
if (interactive()) {
shinyApp(app$ui, app$server)
}