Skip to contents

Usage in teal apps

In this vignette, we will show how to use preprocessing in a teal app. The basics of preprocessing data have been discussed in teal.data here.

In a teal app, providing the code in a copy-paste style is cumbersome and can lead to an out-of-sync situation where the code does not represent the preprocessing code anymore. We therefore use the get_code function to extract the preprocessing code from the app.R file. The get_code function requires # tags to indicate which lines of code in app.R need be included in the preprocessing code. get_code understands the following tags:

  • Enclosing preprocessing code between #code> #<code determines the start and end of preprocessing. Only one start and one end is allowed. If start or end are not specified then preprocessing starts at the beginning of the file and ends at the end respectively.
  • #nocode at the end of the line excludes this line from preprocessing.
  • Lines enclosed within #nocode> #<nocode are excluded from preprocessing.

Further, the get_code function has the following arguments that might be useful:

  • exclude_comments = TRUE removes comments from preprocessing code.
  • read_sources = TRUE means that code from sourced file will be included instead of source() call.

To complete the above example try the code below (NOTE: make sure to save the code in a file called app.R):

# Code needs modification before it can be run:
#     - save as app.R
# for the purpose of example save the file to current directory
library(scda)
library(teal)
synthetic_cdisc_data("latest")$adsl
saveRDS(synthetic_cdisc_data("latest")$adsl, "adsl.rds")
# code>
adsl <- readRDS("adsl.rds")
# <code

cs_arm <- choices_selected(c("ACTARMCD", "ARMCD"), "ACTARMCD")

app <- init(
  data = cdisc_data(cdisc_dataset("ADSL", adsl), code = get_code(file = "app.R")),
  modules = example_module()
)

shinyApp(app$ui, app$server)

Also try the following more advanced usage of get_code:

# Code needs modification before it can be run:
#     - save as app.R
library(scda)
library(teal)

# code>
# data import
adsl <- synthetic_cdisc_data("latest")$adsl

excluded_obj1 <- 1:10 # nocode

# nocode>
excluded_obj2 <- 1:10
# <nocode

# <code

x <- init(
  data = cdisc_data(cdisc_dataset("ADSL", adsl),
    code = get_code("app.R", exclude_comments = TRUE, read_sources = TRUE),
    check = TRUE
  ),
  modules = example_module(),
  header = "Simple app with preprocessing",
  footer = tags$p(class = "text-muted", "Source: agile-R website")
)

shinyApp(x$ui, x$server)

If you set check = TRUE in cdisc_data then the data provided to the application is checked against the data obtained by evaluating the code in an isolated environment. If the datasets do not match then cdisc_data returns an error.

cdisc_data also analyzes the arguments passed to it. Note that additional transformation within cdisc_data call can break reproducibility and is not allowed, e.g. developers shouldn’t do an operation like ADSL = mutate(ADSL, a = 1) inside cdisc_data.