Data module for teal transformations and output customization
Source: R/teal_transform_module.R
teal_transform_module.Rdteal_transform_module provides a shiny module that enables data transformations within a teal application
and allows for customization of outputs generated by modules.
Usage
teal_transform_module(
ui = NULL,
server = function(id, data) data,
label = "transform module",
datanames = "all"
)Arguments
- ui
(
function(id))shinymodule UI function; must only takeidargument- server
(
function(id, data)orexpression) Ashinymodule server function that takesidanddataas arguments, whereidis the module id anddatais the reactiveteal_datainput. Theserverfunction must return a reactive expression containing ateal_dataobject. For simplified syntax, usemake_teal_transform_server().- label
(
character(1)) Label of the module.- datanames
(
character) Specifies the names of datasets relevant to the module. Only filters for the specifieddatanameswill be displayed in the filter panel. The keyword"all"can be used to display filters for all datasets.datanamesare automatically appended to themodules()datanames.
Transforming Module Inputs in teal
Data transformations occur after data has been filtered in teal.
The transformed data is then passed to the server of teal_module() and managed by teal's internal processes.
The primary advantage of teal_transform_module over custom modules is in its error handling, where all warnings and
errors are managed by teal, allowing developers to focus on transformation logic.
For more details, see the vignette: vignette("transform-input-data", package = "teal").
Customizing Module Outputs
teal_transform_module also allows developers to modify any object created within teal.data::teal_data.
This means you can use it to customize not only datasets but also tables, listings, and graphs.
Some teal_modules permit developers to inject custom shiny modules to enhance displayed outputs.
To manage these decorators within your module, use ui_transform_teal_data() and srv_transform_teal_data().
(For further guidance on managing decorators, refer to ui_args and srv_args in the vignette documentation.)
See the vignette vignette("transform-module-output", package = "teal") for additional examples.
server as a language
The server function in teal_transform_module must return a reactive teal.data::teal_data object.
For simple transformations without complex reactivity, the server function might look like this:s
function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
within(
data(),
expr = x <- subset(x, col == level),
level = input$level
)
})
})
}
The example above can be simplified using make_teal_transform_server, where level is automatically matched to the
corresponding input parameter:
Examples
data_transformators <- list(
teal_transform_module(
label = "Static transformator for iris",
datanames = "iris",
server = function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
within(data(), {
iris <- head(iris, 5)
})
})
})
}
),
teal_transform_module(
label = "Interactive transformator for iris",
datanames = "iris",
ui = function(id) {
ns <- NS(id)
tags$div(
numericInput(ns("n_cols"), "Show n columns", value = 5, min = 1, max = 5, step = 1)
)
},
server = function(id, data) {
moduleServer(id, function(input, output, session) {
reactive({
within(data(),
{
iris <- iris[, 1:n_cols]
},
n_cols = input$n_cols
)
})
})
}
)
)
output_decorator <- teal_transform_module(
server = make_teal_transform_server(
expression(
object <- rev(object)
)
)
)
app <- init(
data = teal_data(iris = iris),
modules = example_module(
transformators = data_transformators,
decorators = list(output_decorator)
)
)
if (interactive()) {
shinyApp(app$ui, app$server)
}