View source: R/teal_transform_module.R
| teal_transform_module | R Documentation | 
teal transformations and output customizationteal_transform_module provides a shiny module that enables data transformations within a teal application
and allows for customization of outputs generated by modules.
teal_transform_module(
  ui = NULL,
  server = function(id, data) data,
  label = "transform module",
  datanames = "all"
)
ui | 
 (  | 
server | 
 (  | 
label | 
 (  | 
datanames | 
 (  | 
tealData 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").
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 languageThe 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:
make_teal_transform_server(expr = expression(x <- subset(x, col == level)))
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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.