Proper functioning of any teal
application requires the
presence of a teal_data
object. Typically, a
teal_data
object created in the global environment will be
passed to the data
argument in init
. This
teal_data
object should contain all elements necessary for
successful execution of the application’s modules.
In some scenarios, however, application developers may opt to
postpone some data operations until the application runtime. This can be
done by passing a special shiny
module to the
data
argument. The teal_data_module
function
is used to build such a module from the following components:
id
; defines
user interface elements for the data moduleid
;
defines server logic for the data module, including data creation; must
return a reactive expression containing a teal_data
objectteal
will run this module when the application starts
and the resulting teal_data
object that will be used
throughout all teal
(analytic) modules.
One case for postponing data operations is datasets that are dynamic,
frequently updated. Such data cannot be created once and kept in the
global environment. Using teal_data_module
enables creating
a dataset from scratch every time the user starts the application.
library(teal)
<- teal_data_module(
data_module ui = function(id) div(),
server = function(id) {
moduleServer(id, function(input, output, session) {
reactive({
<- within(
data teal_data(),
{<- iris
dataset1 <- mtcars
dataset2
}
)datanames(data) <- c("dataset1", "dataset2") # optional
data
})
})
}
)
<- init(
app data = data_module,
modules = example_module()
)
if (interactive()) {
shinyApp(app$ui, app$server)
}
See ?qenv
for a detailed explanation of how to use
the within
method.
Another reason to postpone data operations is to involve the application user in the preprocessing stage. An initial, constant form of the data can be created in the global environment and then modified once the app starts.
The following example illustrates how teal_data_module
can be utilized to subset data based on the user inputs:
<- within(teal_data(), {
data <- iris
dataset1 <- mtcars
dataset2
})datanames(data) <- c("dataset1", "dataset2")
<- teal_data_module(
data_module ui = function(id) {
<- NS(id)
ns div(
selectInput(ns("species"), "Select species to keep",
choices = unique(iris$Species), multiple = TRUE
),actionButton(ns("submit"), "Submit")
)
},server = function(id) {
moduleServer(id, function(input, output, session) {
eventReactive(input$submit, {
<- within(
data_modified
data,<- subset(dataset1, Species %in% selected),
dataset1 selected = input$species
)
data_modified
})
})
}
)
<- init(
app data = data_module,
modules = example_module()
)
if (interactive()) {
shinyApp(app$ui, app$server)
}
Note that running preprocessing code in a module as opposed to the global environment will increase app loading times. It is recommended to keep the constant code in the global environment and to move only the dynamic parts to a data module.
When using teal_data_module
to modify a pre-existing
teal_data
object, it is crucial that the server function
and the data object are defined in the same environment, otherwise the
server function will not be able to access the data object. This means
server functions defined in packages cannot be used.
teal_data_modules
The server logic of a teal_data_module
can be modified
before it is used in an app, using the within
function.
This allows the teal_data
object that is created in the
teal_data_module
to be processed further.
In the previous example, data_module
takes a predefined
teal_data
object and allows the app user to select a
subset. The following example modifies data_module
so that
new columns are added once the data is retrieved.
<- within(
data_module_2
data_module,
{# Create new column with Ratio of Sepal.Width and Petal.Width
$Ratio.Sepal.Petal.Width <- round(dataset1$Sepal.Width / dataset1$Petal.Width, digits = 2L)
dataset1# Create new column that converts Miles per Galon to Liter per 100 Km
$lp100km <- round(dataset2$mpg * 0.42514371, digits = 2L)
dataset2
}
)
<- init(
app data = data_module_2,
modules = example_module()
)
if (interactive()) {
shinyApp(app$ui, app$server)
}