teal_data_module: Data module for 'teal' applications

View source: R/teal_data_module.R

teal_data_moduleR Documentation

Data module for teal applications

Description

[Experimental]

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

Usage

teal_data_module(ui, server)

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

## S3 method for class '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.

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.

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.

See Also

teal.data::teal_data, teal.code::qenv()

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')")

within(tdm, dataset1 <- subset(dataset1, Species == "virginica"))

# use additional parameter for expression value substitution.
valid_species <- "versicolor"
within(tdm, dataset1 <- subset(dataset1, Species %in% species), species = valid_species)

teal documentation built on May 29, 2024, 10:11 a.m.