A common scenario when using shinyCohortBuilder
in
custom Shiny application is to allow a user to switch between multiple
data sources.
Such scenario takes place when you:
The goal of this document is to explain how
shinyCohortBuilder
can be used in the two scenarios.
Whenever you use different data in your Shiny application you should
use cohortBuilder::update_source
method to update
underlying data.
The method not only replaces Source in the Cohort object, but also triggers set of specific operations in filtering panel.
The operations depends on the way you store filters configuration and extra options passed to the method.
Below we describe two most common solutions when dealing with the above scenarios.
When working with consistent datasets you usually want to keep filtering panel unchanged for each data version. In this situation:
update_source(cohort_object, keep_steps = TRUE)
.As a result:
Below application presents the described approach:
library(shiny)
library(cohortBuilder)
library(shinyCohortBuilder)
= list(
mtcars_list "0" = dplyr::filter(mtcars, am == 0),
"1" = dplyr::filter(mtcars, am == 1)
)
<- fluidPage(
ui sidebarLayout(
sidebarPanel(
radioButtons("version", "Version", choices = c("0", "1")),
cb_ui("mtcars")
),mainPanel(
verbatimTextOutput("cohort_data")
)
)
)
<- function(input, output, session) {
server <- set_source(tblist(mtcars = mtcars_list[["0"]]))
init_source <- cohort(
mt_cohort
init_source,filter("range", id = "mpg", dataset = "mtcars", variable = "mpg", active = FALSE),
filter("range", id = "qsec", dataset = "mtcars", variable = "qsec", active = FALSE)
)
cb_server("mtcars", mt_cohort)
observeEvent(input$version, {
<- set_source(tblist(mtcars = mtcars_list[[input$version]]))
new_source update_source(mt_cohort, new_source, keep_steps = TRUE)
})
}
shinyApp(ui, server)
When filters configuration is different across multiple data sources you want to render the filtering panel from scratch every time the source is updated. In this scenario:
update_source(cohort_object, keep_steps = FALSE)
.As a result:
Below application presents the described approach:
library(shiny)
library(cohortBuilder)
library(shinyCohortBuilder)
<- set_source(
source_mtcars tblist(mtcars = mtcars),
filter("range", id = "mpg", dataset = "mtcars", variable = "mpg", active = FALSE),
filter("range", id = "qsec", dataset = "mtcars", variable = "qsec", active = FALSE)
)
<- set_source(
source_iris tblist(iris = iris),
filter("discrete", id = "species", dataset = "iris", variable = "Species", active = FALSE),
filter("range", id = "petal_length", dataset = "iris", variable = "Petal.length", active = FALSE)
)
<- fluidPage(
ui sidebarLayout(
sidebarPanel(
radioButtons("dataset", "Dataset", choices = c("mtcars", "iris")),
cb_ui("data_panel")
),mainPanel(
verbatimTextOutput("cohort_data")
)
)
)
<- function(input, output, session) {
server <- cohort(source_mtcars)
cohort_object cb_server("data_panel", cohort_object)
observeEvent(input$dataset, {
if (input$dataset = "mtcars") {
update_source(cohort_object, source_mtcars, keep_steps = FALSE)
}if (input$dataset = "iris") {
update_source(cohort_object, source_iris, keep_steps = FALSE)
}
})
}
shinyApp(ui, server)