Skip to contents

[Experimental]

Create a teal_data_module object and evaluate code on it with history tracking.

Usage

teal_data_module(ui, server)

# S4 method for teal_data_module,character
eval_code(object, code)

# S3 method for teal_data_module
within(data, expr, ...)

Arguments

ui

(function(id)) shiny module UI function; must only take id argument

server

(function(id)) shiny module server function; must only take id argument; must return reactive expression containing teal_data object

object

(teal_data_module)

code

(character or language) code to evaluate. If character, comments are retained.

data

(teal_data_module) object

expr

(expression) to evaluate. Must be inline code. See

...

See Details.

Value

teal_data_module returns an object of class teal_data_module.

eval_code returns a teal_data_module object with a delayed evaluation of code when the module is run.

within returns a teal_data_module object with a delayed evaluation of expr when the module is run.

Details

teal_data_module creates a shiny module to supply or modify data in a teal application. The module allows for running data pre-processing code (creation and some modification) after the app starts. The body of the server function will be run in the app rather than in the global environment. This means it will be run every time the app starts, so use sparingly.

Pass this module instead of a teal_data object in a call to init(). Note that the server function must always return a teal_data object wrapped in a reactive expression.

See vignette vignette("data-as-shiny-module", package = "teal") for more details.

eval_code evaluates given code in the environment of the teal_data object created by the teal_data_module. The code is added to the @code slot of the teal_data.

within is a convenience function for evaluating inline code inside the environment of a teal_data_module. It accepts only inline expressions (both simple and compound) and allows for injecting values into expr through the ... argument: as name:value pairs are passed to ..., name in expr will be replaced with value.

Examples

tdm <- teal_data_module(
  ui = function(id) {
    ns <- NS(id)
    actionButton(ns("submit"), label = "Load data")
  },
  server = function(id) {
    moduleServer(id, function(input, output, session) {
      eventReactive(input$submit, {
        data <- within(
          teal_data(),
          {
            dataset1 <- iris
            dataset2 <- mtcars
          }
        )
        datanames(data) <- c("dataset1", "dataset2")

        data
      })
    })
  }
)

eval_code(tdm, "dataset1 <- subset(dataset1, Species == 'virginica')")
#> $ui
#> function(id) {
#>       ns <- NS(id)
#>       object$ui(ns("mutate_inner"))
#>     }
#> <environment: 0x55bc4e8b3b88>
#> 
#> $server
#> function(id) {
#>       moduleServer(id, function(input, output, session) {
#>         teal_data_rv <- object$server("mutate_inner")
#> 
#>         if (!is.reactive(teal_data_rv)) {
#>           stop("The `teal_data_module` must return a reactive expression.", call. = FALSE)
#>         }
#> 
#>         td <- eventReactive(teal_data_rv(),
#>           {
#>             if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) {
#>               eval_code(teal_data_rv(), code)
#>             } else {
#>               teal_data_rv()
#>             }
#>           },
#>           ignoreNULL = FALSE
#>         )
#>         td
#>       })
#>     }
#> <environment: 0x55bc4e8b3b88>
#> 
#> attr(,"class")
#> [1] "teal_data_module"

within(tdm, dataset1 <- subset(dataset1, Species == "virginica"))
#> $ui
#> function(id) {
#>       ns <- NS(id)
#>       object$ui(ns("mutate_inner"))
#>     }
#> <environment: 0x55bc4e655288>
#> 
#> $server
#> function(id) {
#>       moduleServer(id, function(input, output, session) {
#>         teal_data_rv <- object$server("mutate_inner")
#> 
#>         if (!is.reactive(teal_data_rv)) {
#>           stop("The `teal_data_module` must return a reactive expression.", call. = FALSE)
#>         }
#> 
#>         td <- eventReactive(teal_data_rv(),
#>           {
#>             if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) {
#>               eval_code(teal_data_rv(), code)
#>             } else {
#>               teal_data_rv()
#>             }
#>           },
#>           ignoreNULL = FALSE
#>         )
#>         td
#>       })
#>     }
#> <environment: 0x55bc4e655288>
#> 
#> attr(,"class")
#> [1] "teal_data_module"

# use additional parameter for expression value substitution.
valid_species <- "versicolor"
within(tdm, dataset1 <- subset(dataset1, Species %in% species), species = valid_species)
#> $ui
#> function(id) {
#>       ns <- NS(id)
#>       object$ui(ns("mutate_inner"))
#>     }
#> <environment: 0x55bc4e443f48>
#> 
#> $server
#> function(id) {
#>       moduleServer(id, function(input, output, session) {
#>         teal_data_rv <- object$server("mutate_inner")
#> 
#>         if (!is.reactive(teal_data_rv)) {
#>           stop("The `teal_data_module` must return a reactive expression.", call. = FALSE)
#>         }
#> 
#>         td <- eventReactive(teal_data_rv(),
#>           {
#>             if (inherits(teal_data_rv(), c("teal_data", "qenv.error"))) {
#>               eval_code(teal_data_rv(), code)
#>             } else {
#>               teal_data_rv()
#>             }
#>           },
#>           ignoreNULL = FALSE
#>         )
#>         td
#>       })
#>     }
#> <environment: 0x55bc4e443f48>
#> 
#> attr(,"class")
#> [1] "teal_data_module"