Title: | Functions for Extracting and Merging Data in the 'teal' Framework |
---|---|
Description: | A standardized user interface for column selection, that facilitates dataset merging in 'teal' framework. |
Authors: | Dawid Kaledkowski [aut, cre], Pawel Rucki [aut], Mahmoud Hallal [aut], Nikolas Burkoff [aut], Maciej Nasinski [aut], Konrad Pagacz [aut], Junlue Zhao [aut], F. Hoffmann-La Roche AG [cph, fnd] |
Maintainer: | Dawid Kaledkowski <[email protected]> |
License: | Apache License 2.0 |
Version: | 0.5.0 |
Built: | 2024-11-14 03:59:42 UTC |
Source: | https://github.com/insightsengineering/teal.transform |
add_no_selected_choices(x, multiple = FALSE)
add_no_selected_choices(x, multiple = FALSE)
x |
( |
multiple |
( |
choices_selected
object with an empty option added to the choices.
all_choices
objectAn S3 structure representing the selection of all possible choices in a
filter_spec
, select_spec
or choices_selected
object.
all_choices()
all_choices()
all_choices
object.
# Both structures are semantically identical filter_spec( vars = c("selected_variable"), choices = c("value1", "value2"), selected = c("value1", "value2") ) filter_spec( vars = c("selected_variable"), choices = c("value1", "value2"), selected = all_choices() ) choices_selected(choices = letters, selected = letters) choices_selected(choices = letters, selected = all_choices())
# Both structures are semantically identical filter_spec( vars = c("selected_variable"), choices = c("value1", "value2"), selected = c("value1", "value2") ) filter_spec( vars = c("selected_variable"), choices = c("value1", "value2"), selected = all_choices() ) choices_selected(choices = letters, selected = letters) choices_selected(choices = letters, selected = all_choices())
extract_input
specification does not allow multiple
selectioncheck_no_multiple_selection(extract_input)
check_no_multiple_selection(extract_input)
extract_input |
( |
Stops if condition not met.
Raises an error when check fails, otherwise, it returns NULL
, invisibly.
<choice>:<label>
" type of namesThis is often useful for choices_selected()
as it marks up the drop-down boxes
for shiny::selectInput()
.
choices_labeled(choices, labels, subset = NULL, types = NULL) ## S3 method for class 'choices_labeled' print(x, ...)
choices_labeled(choices, labels, subset = NULL, types = NULL) ## S3 method for class 'choices_labeled' print(x, ...)
choices |
( |
labels |
( |
subset |
( |
types |
( |
x |
an object used to select a method. |
... |
further arguments passed to or from other methods. |
If either choices
or labels
are factors, they are coerced to character.
Duplicated elements from choices
get removed.
Named character
vector.
print(choices_labeled)
: Print choices_labeled object
library(shiny) library(teal.data) ADSL <- teal.transform::rADSL ADTTE <- teal.transform::rADTTE choices1 <- choices_labeled(names(ADSL), col_labels(ADSL, fill = FALSE)) choices2 <- choices_labeled(ADTTE$PARAMCD, ADTTE$PARAM) # if only a subset of variables are needed, use subset argument choices3 <- choices_labeled( names(ADSL), col_labels(ADSL, fill = FALSE), subset = c("ARMCD", "ARM") ) ui <- fluidPage( selectInput("c1", label = "Choices from ADSL", choices = choices1, selected = choices1[1] ), selectInput("c2", label = "Choices from ADTTE", choices = choices2, selected = choices2[1] ), selectInput("c3", label = "Arm choices from ADSL", choices = choices3, selected = choices3[1] ) ) server <- function(input, output) {} if (interactive()) { shinyApp(ui, server) }
library(shiny) library(teal.data) ADSL <- teal.transform::rADSL ADTTE <- teal.transform::rADTTE choices1 <- choices_labeled(names(ADSL), col_labels(ADSL, fill = FALSE)) choices2 <- choices_labeled(ADTTE$PARAMCD, ADTTE$PARAM) # if only a subset of variables are needed, use subset argument choices3 <- choices_labeled( names(ADSL), col_labels(ADSL, fill = FALSE), subset = c("ARMCD", "ARM") ) ui <- fluidPage( selectInput("c1", label = "Choices from ADSL", choices = choices1, selected = choices1[1] ), selectInput("c2", label = "Choices from ADTTE", choices = choices2, selected = choices2[1] ), selectInput("c3", label = "Arm choices from ADSL", choices = choices3, selected = choices3[1] ) ) server <- function(input, output) {} if (interactive()) { shinyApp(ui, server) }
Construct a single list containing available choices, the default selected value, and additional settings such as to order the choices with the selected elements appearing first or whether to block the user from making selections.
Can be used in UI input elements such as teal.widgets::optionalSelectInput()
.
choices_selected( choices, selected = if (inherits(choices, "delayed_data")) NULL else choices[1], keep_order = FALSE, fixed = FALSE ) is.choices_selected(x)
choices_selected( choices, selected = if (inherits(choices, "delayed_data")) NULL else choices[1], keep_order = FALSE, fixed = FALSE ) is.choices_selected(x)
choices |
( See |
selected |
( If |
keep_order |
( |
fixed |
(optional |
x |
( |
Please note that the order of selected will always follow the order of choices. The keep_order
argument is set to false which will run the following code inside:
choices <- c(selected, setdiff(choices, selected))
In case you want to keep your specific order of choices, set keep_order
to TRUE
.
choices_selected
returns list of choices_selected
, encapsulating the specified
choices
, selected
, keep_order
and fixed
.
is.choices_selected
returns TRUE
if x
inherits from a choices_selected
object, FALSE
otherwise.
is.choices_selected()
: Check if an object is a choices_selected class
library(shiny) library(teal.widgets) # all_choices example - semantically the same objects choices_selected(choices = letters, selected = all_choices()) choices_selected(choices = letters, selected = letters) choices_selected( choices = setNames(LETTERS[1:5], paste("Letter", LETTERS[1:5])), selected = "C" ) ADSL <- teal.transform::rADSL choices_selected(variable_choices(ADSL), "SEX") # How to select nothing # use an empty character choices_selected( choices = c("", "A", "B", "C"), selected = "" ) # How to allow the user to select nothing # use an empty character choices_selected( choices = c("A", "", "B", "C"), selected = "A" ) # How to make Nothing the Xth choice # just use keep_order choices_selected( choices = c("A", "", "B", "C"), selected = "A", keep_order = TRUE ) # How to give labels to selections # by adding names - choices will be replaced by "name" in UI, not in code choices_selected( choices = c("name for A" = "A", "Name for nothing" = "", "name for b" = "B", "name for C" = "C"), selected = "A" ) # by using choices_labeled # labels will be shown behind the choice choices_selected( choices = choices_labeled( c("A", "", "B", "C"), c("name for A", "nothing", "name for B", "name for C") ), selected = "A" ) # Passing a `delayed_data` object to `selected` choices_selected( choices = variable_choices("ADSL"), selected = variable_choices("ADSL", subset = c("STUDYID")) ) # functional form (subsetting for factor variables only) of choices_selected # with delayed data loading choices_selected(variable_choices("ADSL", subset = function(data) { idx <- vapply(data, is.factor, logical(1)) names(data)[idx] })) cs <- choices_selected( choices = c("A", "B", "C"), selected = "A" ) ui <- fluidPage( optionalSelectInput( inputId = "id", choices = cs$choices, selected = cs$selected ) ) server <- function(input, output, session) {} if (interactive()) { shinyApp(ui, server) }
library(shiny) library(teal.widgets) # all_choices example - semantically the same objects choices_selected(choices = letters, selected = all_choices()) choices_selected(choices = letters, selected = letters) choices_selected( choices = setNames(LETTERS[1:5], paste("Letter", LETTERS[1:5])), selected = "C" ) ADSL <- teal.transform::rADSL choices_selected(variable_choices(ADSL), "SEX") # How to select nothing # use an empty character choices_selected( choices = c("", "A", "B", "C"), selected = "" ) # How to allow the user to select nothing # use an empty character choices_selected( choices = c("A", "", "B", "C"), selected = "A" ) # How to make Nothing the Xth choice # just use keep_order choices_selected( choices = c("A", "", "B", "C"), selected = "A", keep_order = TRUE ) # How to give labels to selections # by adding names - choices will be replaced by "name" in UI, not in code choices_selected( choices = c("name for A" = "A", "Name for nothing" = "", "name for b" = "B", "name for C" = "C"), selected = "A" ) # by using choices_labeled # labels will be shown behind the choice choices_selected( choices = choices_labeled( c("A", "", "B", "C"), c("name for A", "nothing", "name for B", "name for C") ), selected = "A" ) # Passing a `delayed_data` object to `selected` choices_selected( choices = variable_choices("ADSL"), selected = variable_choices("ADSL", subset = c("STUDYID")) ) # functional form (subsetting for factor variables only) of choices_selected # with delayed data loading choices_selected(variable_choices("ADSL", subset = function(data) { idx <- vapply(data, is.factor, logical(1)) names(data)[idx] })) cs <- choices_selected( choices = c("A", "B", "C"), selected = "A" ) ui <- fluidPage( optionalSelectInput( inputId = "id", choices = cs$choices, selected = cs$selected ) ) server <- function(input, output, session) {} if (interactive()) { shinyApp(ui, server) }
validators
from data_extract_multiple_srv
This function takes the output from data_extract_multiple_srv
and
collates the shinyvalidate::InputValidator
s returned into a single
validator
and enables this.
compose_and_enable_validators(iv, selector_list, validator_names = NULL)
compose_and_enable_validators(iv, selector_list, validator_names = NULL)
iv |
( |
selector_list |
( |
validator_names |
( |
(shinyvalidate::InputValidator
) enabled iv
with appropriate validators
added into it.
library(shiny) library(shinyvalidate) library(shinyjs) library(teal.widgets) iris_extract <- data_extract_spec( dataname = "iris", select = select_spec( label = "Select variable:", choices = variable_choices(iris, colnames(iris)), selected = "Sepal.Length", multiple = TRUE, fixed = FALSE ) ) data_list <- list(iris = reactive(iris)) ui <- fluidPage( useShinyjs(), standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "x_var", label = "Please select an X column", data_extract_spec = iris_extract ), data_extract_ui( id = "y_var", label = "Please select a Y column", data_extract_spec = iris_extract ), data_extract_ui( id = "col_var", label = "Please select a color column", data_extract_spec = iris_extract ) ) ) ) server <- function(input, output, session) { exactly_2_validation <- function() { ~ if (length(.) != 2) "Exactly 2 'Y' column variables must be chosen" } selector_list <- data_extract_multiple_srv( list(x_var = iris_extract, y_var = iris_extract, col_var = iris_extract), datasets = data_list, select_validation_rule = list( x_var = sv_required("Please select an X column"), y_var = compose_rules( sv_required("Exactly 2 'Y' column variables must be chosen"), exactly_2_validation() ) ) ) iv_r <- reactive({ iv <- InputValidator$new() compose_and_enable_validators( iv, selector_list, # if validator_names = NULL then all validators are used # to turn on only "x_var" then set this argument to "x_var" validator_names = NULL ) }) output$out1 <- renderPrint({ if (iv_r()$is_valid()) { ans <- lapply(selector_list(), function(x) { cat(format_data_extract(x()), "\n\n") }) } else { "Check that you have made a valid selection" } }) } if (interactive()) { shinyApp(ui, server) }
library(shiny) library(shinyvalidate) library(shinyjs) library(teal.widgets) iris_extract <- data_extract_spec( dataname = "iris", select = select_spec( label = "Select variable:", choices = variable_choices(iris, colnames(iris)), selected = "Sepal.Length", multiple = TRUE, fixed = FALSE ) ) data_list <- list(iris = reactive(iris)) ui <- fluidPage( useShinyjs(), standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "x_var", label = "Please select an X column", data_extract_spec = iris_extract ), data_extract_ui( id = "y_var", label = "Please select a Y column", data_extract_spec = iris_extract ), data_extract_ui( id = "col_var", label = "Please select a color column", data_extract_spec = iris_extract ) ) ) ) server <- function(input, output, session) { exactly_2_validation <- function() { ~ if (length(.) != 2) "Exactly 2 'Y' column variables must be chosen" } selector_list <- data_extract_multiple_srv( list(x_var = iris_extract, y_var = iris_extract, col_var = iris_extract), datasets = data_list, select_validation_rule = list( x_var = sv_required("Please select an X column"), y_var = compose_rules( sv_required("Exactly 2 'Y' column variables must be chosen"), exactly_2_validation() ) ) ) iv_r <- reactive({ iv <- InputValidator$new() compose_and_enable_validators( iv, selector_list, # if validator_names = NULL then all validators are used # to turn on only "x_var" then set this argument to "x_var" validator_names = NULL ) }) output$out1 <- renderPrint({ if (iv_r()$is_valid()) { ans <- lapply(selector_list(), function(x) { cat(format_data_extract(x()), "\n\n") }) } else { "Check that you have made a valid selection" } }) } if (interactive()) { shinyApp(ui, server) }
data_extract_srv
outputdata_extract_multiple_srv
loops over the list of data_extract
given and
runs data_extract_srv
for each one returning a list of reactive objects.
data_extract_multiple_srv(data_extract, datasets, ...) ## S3 method for class 'reactive' data_extract_multiple_srv(data_extract, datasets, ...) ## S3 method for class 'FilteredData' data_extract_multiple_srv(data_extract, datasets, ...) ## S3 method for class 'list' data_extract_multiple_srv( data_extract, datasets, join_keys = NULL, select_validation_rule = NULL, filter_validation_rule = NULL, dataset_validation_rule = if (is.null(select_validation_rule) && is.null(filter_validation_rule)) { NULL } else { shinyvalidate::sv_required("Please select a dataset") }, ... )
data_extract_multiple_srv(data_extract, datasets, ...) ## S3 method for class 'reactive' data_extract_multiple_srv(data_extract, datasets, ...) ## S3 method for class 'FilteredData' data_extract_multiple_srv(data_extract, datasets, ...) ## S3 method for class 'list' data_extract_multiple_srv( data_extract, datasets, join_keys = NULL, select_validation_rule = NULL, filter_validation_rule = NULL, dataset_validation_rule = if (is.null(select_validation_rule) && is.null(filter_validation_rule)) { NULL } else { shinyvalidate::sv_required("Please select a dataset") }, ... )
data_extract |
(named See example for details. |
datasets |
( |
... |
An additional argument |
join_keys |
( |
select_validation_rule |
( For more fine-grained control use a list:
If See example for more details. |
filter_validation_rule |
( |
dataset_validation_rule |
( |
reactive named list
containing outputs from data_extract_srv()
.
Output list names are the same as data_extract
input argument.
library(shiny) library(shinyvalidate) library(shinyjs) library(teal.widgets) iris_select <- data_extract_spec( dataname = "iris", select = select_spec( label = "Select variable:", choices = variable_choices(iris, colnames(iris)), selected = "Sepal.Length", multiple = TRUE, fixed = FALSE ) ) iris_filter <- data_extract_spec( dataname = "iris", filter = filter_spec( vars = "Species", choices = c("setosa", "versicolor", "virginica"), selected = "setosa", multiple = TRUE ) ) data_list <- list(iris = reactive(iris)) ui <- fluidPage( useShinyjs(), standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "x_var", label = "Please select an X column", data_extract_spec = iris_select ), data_extract_ui( id = "species_var", label = "Please select 2 Species", data_extract_spec = iris_filter ) ) ) ) server <- function(input, output, session) { exactly_2_validation <- function(msg) { ~ if (length(.) != 2) msg } selector_list <- data_extract_multiple_srv( list(x_var = iris_select, species_var = iris_filter), datasets = data_list, select_validation_rule = list( x_var = sv_required("Please select an X column") ), filter_validation_rule = list( species_var = compose_rules( sv_required("Exactly 2 Species must be chosen"), exactly_2_validation("Exactly 2 Species must be chosen") ) ) ) iv_r <- reactive({ iv <- InputValidator$new() compose_and_enable_validators( iv, selector_list, validator_names = NULL ) }) output$out1 <- renderPrint({ if (iv_r()$is_valid()) { ans <- lapply(selector_list(), function(x) { cat(format_data_extract(x()), "\n\n") }) } else { "Please fix errors in your selection" } }) } if (interactive()) { shinyApp(ui, server) }
library(shiny) library(shinyvalidate) library(shinyjs) library(teal.widgets) iris_select <- data_extract_spec( dataname = "iris", select = select_spec( label = "Select variable:", choices = variable_choices(iris, colnames(iris)), selected = "Sepal.Length", multiple = TRUE, fixed = FALSE ) ) iris_filter <- data_extract_spec( dataname = "iris", filter = filter_spec( vars = "Species", choices = c("setosa", "versicolor", "virginica"), selected = "setosa", multiple = TRUE ) ) data_list <- list(iris = reactive(iris)) ui <- fluidPage( useShinyjs(), standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "x_var", label = "Please select an X column", data_extract_spec = iris_select ), data_extract_ui( id = "species_var", label = "Please select 2 Species", data_extract_spec = iris_filter ) ) ) ) server <- function(input, output, session) { exactly_2_validation <- function(msg) { ~ if (length(.) != 2) msg } selector_list <- data_extract_multiple_srv( list(x_var = iris_select, species_var = iris_filter), datasets = data_list, select_validation_rule = list( x_var = sv_required("Please select an X column") ), filter_validation_rule = list( species_var = compose_rules( sv_required("Exactly 2 Species must be chosen"), exactly_2_validation("Exactly 2 Species must be chosen") ) ) ) iv_r <- reactive({ iv <- InputValidator$new() compose_and_enable_validators( iv, selector_list, validator_names = NULL ) }) output$out1 <- renderPrint({ if (iv_r()$is_valid()) { ans <- lapply(selector_list(), function(x) { cat(format_data_extract(x()), "\n\n") }) } else { "Please fix errors in your selection" } }) } if (interactive()) { shinyApp(ui, server) }
teal
modulesThe Data extract input can be used to filter and select columns from a data set.
This function enables such an input in teal
.
Please use the constructor function data_extract_spec to set it up.
data_extract_spec(dataname, select = NULL, filter = NULL, reshape = FALSE)
data_extract_spec(dataname, select = NULL, filter = NULL, reshape = FALSE)
dataname |
( |
select |
( |
filter |
( |
reshape |
( |
data_extract_spec
object.
teal.transform
uses this object to construct a UI element in a module.
No checks based on columns can be done because the data is only referred to by name.
adtte_filters <- filter_spec( vars = c("PARAMCD", "CNSR"), sep = "-", choices = c("OS-1" = "OS-1", "OS-0" = "OS-0", "PFS-1" = "PFS-1"), selected = "OS-1", multiple = FALSE, label = "Choose endpoint and Censor" ) data_extract_spec( dataname = "ADTTE", filter = adtte_filters, select = select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = FALSE, label = "Column" ) ) data_extract_spec( dataname = "ADSL", filter = NULL, select = select_spec( choices = c("AGE", "SEX", "USUBJID"), selected = c("SEX"), multiple = FALSE, fixed = FALSE ) ) data_extract_spec( dataname = "ADSL", filter = filter_spec( vars = variable_choices("ADSL", subset = c("AGE")) ) ) dynamic_filter <- filter_spec( vars = choices_selected(variable_choices("ADSL"), "COUNTRY"), multiple = TRUE ) data_extract_spec( dataname = "ADSL", filter = dynamic_filter )
adtte_filters <- filter_spec( vars = c("PARAMCD", "CNSR"), sep = "-", choices = c("OS-1" = "OS-1", "OS-0" = "OS-0", "PFS-1" = "PFS-1"), selected = "OS-1", multiple = FALSE, label = "Choose endpoint and Censor" ) data_extract_spec( dataname = "ADTTE", filter = adtte_filters, select = select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = FALSE, label = "Column" ) ) data_extract_spec( dataname = "ADSL", filter = NULL, select = select_spec( choices = c("AGE", "SEX", "USUBJID"), selected = c("SEX"), multiple = FALSE, fixed = FALSE ) ) data_extract_spec( dataname = "ADSL", filter = filter_spec( vars = variable_choices("ADSL", subset = c("AGE")) ) ) dynamic_filter <- filter_spec( vars = choices_selected(variable_choices("ADSL"), "COUNTRY"), multiple = TRUE ) data_extract_spec( dataname = "ADSL", filter = dynamic_filter )
Extracting details of the selection(s) in data_extract_ui elements.
data_extract_srv(id, datasets, data_extract_spec, ...) ## S3 method for class 'FilteredData' data_extract_srv(id, datasets, data_extract_spec, ...) ## S3 method for class 'list' data_extract_srv( id, datasets, data_extract_spec, join_keys = NULL, select_validation_rule = NULL, filter_validation_rule = NULL, dataset_validation_rule = if (is.null(select_validation_rule) && is.null(filter_validation_rule)) { NULL } else { shinyvalidate::sv_required("Please select a dataset") }, ... )
data_extract_srv(id, datasets, data_extract_spec, ...) ## S3 method for class 'FilteredData' data_extract_srv(id, datasets, data_extract_spec, ...) ## S3 method for class 'list' data_extract_srv( id, datasets, data_extract_spec, join_keys = NULL, select_validation_rule = NULL, filter_validation_rule = NULL, dataset_validation_rule = if (is.null(select_validation_rule) && is.null(filter_validation_rule)) { NULL } else { shinyvalidate::sv_required("Please select a dataset") }, ... )
id |
An ID string that corresponds with the ID used to call the module's UI function. |
datasets |
( |
data_extract_spec |
( |
... |
An additional argument |
join_keys |
( |
select_validation_rule |
( You can use a validation function directly (i.e.
If |
filter_validation_rule |
( |
dataset_validation_rule |
( |
A reactive list
containing following fields:
filters
: A list with the information on the filters that are applied to the data set.
select
: The variables that are selected from the dataset.
always_selected
: The column names from the data set that should always be selected.
reshape
: Whether reshape long to wide should be applied or not.
dataname
: The name of the data set.
internal_id
: The id
of the corresponding shiny input element.
keys
: The names of the columns that can be used to merge the data set.
iv
: A shinyvalidate::InputValidator
containing validator
for this data_extract
.
library(shiny) library(shinyvalidate) library(teal.data) library(teal.widgets) # Sample ADSL dataset ADSL <- data.frame( STUDYID = "A", USUBJID = LETTERS[1:10], SEX = rep(c("F", "M"), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) ) # Specification for data extraction adsl_extract <- data_extract_spec( dataname = "ADSL", filter = filter_spec(vars = "SEX", choices = c("F", "M"), selected = "F"), select = select_spec( label = "Select variable:", choices = variable_choices(ADSL, c("AGE", "BMRKR1")), selected = "AGE", multiple = TRUE, fixed = FALSE ) ) # Using reactive list of data.frames data_list <- list(ADSL = reactive(ADSL)) join_keys <- join_keys(join_key("ADSL", "ADSL", c("STUDYID", "USUBJID"))) # App: data extraction with validation ui <- fluidPage( standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "adsl_var", label = "ADSL selection", data_extract_spec = adsl_extract ) ) ) ) server <- function(input, output, session) { adsl_reactive_input <- data_extract_srv( id = "adsl_var", datasets = data_list, data_extract_spec = adsl_extract, join_keys = join_keys, select_validation_rule = sv_required("Please select a variable.") ) iv_r <- reactive({ iv <- InputValidator$new() iv$add_validator(adsl_reactive_input()$iv) iv$enable() iv }) output$out1 <- renderPrint({ if (iv_r()$is_valid()) { cat(format_data_extract(adsl_reactive_input())) } else { "Please fix errors in your selection" } }) } if (interactive()) { shinyApp(ui, server) } # App: simplified data extraction ui <- fluidPage( standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "adsl_var", label = "ADSL selection", data_extract_spec = adsl_extract ) ) ) ) server <- function(input, output, session) { adsl_reactive_input <- data_extract_srv( id = "adsl_var", datasets = data_list, data_extract_spec = adsl_extract ) output$out1 <- renderPrint(adsl_reactive_input()) } if (interactive()) { shinyApp(ui, server) }
library(shiny) library(shinyvalidate) library(teal.data) library(teal.widgets) # Sample ADSL dataset ADSL <- data.frame( STUDYID = "A", USUBJID = LETTERS[1:10], SEX = rep(c("F", "M"), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) ) # Specification for data extraction adsl_extract <- data_extract_spec( dataname = "ADSL", filter = filter_spec(vars = "SEX", choices = c("F", "M"), selected = "F"), select = select_spec( label = "Select variable:", choices = variable_choices(ADSL, c("AGE", "BMRKR1")), selected = "AGE", multiple = TRUE, fixed = FALSE ) ) # Using reactive list of data.frames data_list <- list(ADSL = reactive(ADSL)) join_keys <- join_keys(join_key("ADSL", "ADSL", c("STUDYID", "USUBJID"))) # App: data extraction with validation ui <- fluidPage( standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "adsl_var", label = "ADSL selection", data_extract_spec = adsl_extract ) ) ) ) server <- function(input, output, session) { adsl_reactive_input <- data_extract_srv( id = "adsl_var", datasets = data_list, data_extract_spec = adsl_extract, join_keys = join_keys, select_validation_rule = sv_required("Please select a variable.") ) iv_r <- reactive({ iv <- InputValidator$new() iv$add_validator(adsl_reactive_input()$iv) iv$enable() iv }) output$out1 <- renderPrint({ if (iv_r()$is_valid()) { cat(format_data_extract(adsl_reactive_input())) } else { "Please fix errors in your selection" } }) } if (interactive()) { shinyApp(ui, server) } # App: simplified data extraction ui <- fluidPage( standard_layout( output = verbatimTextOutput("out1"), encoding = tagList( data_extract_ui( id = "adsl_var", label = "ADSL selection", data_extract_spec = adsl_extract ) ) ) ) server <- function(input, output, session) { adsl_reactive_input <- data_extract_srv( id = "adsl_var", datasets = data_list, data_extract_spec = adsl_extract ) output$out1 <- renderPrint(adsl_reactive_input()) } if (interactive()) { shinyApp(ui, server) }
teal
data extraction module user-interfacedata_extract_ui(id, label, data_extract_spec, is_single_dataset = FALSE)
data_extract_ui(id, label, data_extract_spec, is_single_dataset = FALSE)
id |
( |
label |
( |
data_extract_spec |
( |
is_single_dataset |
( |
There are three inputs that will be rendered
Dataset select Optional. If more than one data_extract_spec is handed over to the function, a shiny shiny::selectInput will be rendered. Else just the name of the dataset is given.
Filter Panel Optional. If the data_extract_spec contains a filter element a shiny shiny::selectInput will be rendered with the options to filter the dataset.
Select panel A shiny shiny::selectInput to select columns from the dataset to go into the analysis.
The output can be analyzed using data_extract_srv(...)
.
This functionality should be used in the encoding panel of your teal
app.
It will allow app-developers to specify a data_extract_spec()
object.
This object should be used to teal
module variables being filtered data
from CDISC datasets.
You can use this function in the same way as any
shiny module
UI.
The corresponding server module can be found in data_extract_srv()
.
Shiny shiny::selectInput
s
that allow to define how to extract data from
a specific dataset. The input elements will be returned inside a shiny::div container.
library(shiny) library(teal.widgets) adtte_filters <- filter_spec( vars = c("PARAMCD", "CNSR"), sep = "-", choices = c("OS-1" = "OS-1", "OS-0" = "OS-0", "PFS-1" = "PFS-1"), selected = "OS-1", multiple = FALSE, label = "Choose endpoint and Censor" ) response_spec <- data_extract_spec( dataname = "ADTTE", filter = adtte_filters, select = select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = FALSE, label = "Column" ) ) # Call to use inside your teal module UI function standard_layout( output = tableOutput("table"), encoding = div( data_extract_ui( id = "regressor", label = "Regressor Variable", data_extract_spec = response_spec ) ) )
library(shiny) library(teal.widgets) adtte_filters <- filter_spec( vars = c("PARAMCD", "CNSR"), sep = "-", choices = c("OS-1" = "OS-1", "OS-0" = "OS-0", "PFS-1" = "PFS-1"), selected = "OS-1", multiple = FALSE, label = "Choose endpoint and Censor" ) response_spec <- data_extract_spec( dataname = "ADTTE", filter = adtte_filters, select = select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = FALSE, label = "Column" ) ) # Call to use inside your teal module UI function standard_layout( output = tableOutput("table"), encoding = div( data_extract_ui( id = "regressor", label = "Regressor Variable", data_extract_spec = response_spec ) ) )
Creates shiny::helpText()
with the names of available datasets for the
current module.
datanames_input(data_extracts)
datanames_input(data_extracts)
data_extracts |
( |
shiny.tag
defining help-text element that can be added to a UI element.
It consists in choices and additionally the variable names for the choices.
filter_spec( vars, choices = NULL, selected = if (inherits(choices, "delayed_data")) NULL else choices[1], multiple = length(selected) > 1 || inherits(selected, "all_choices"), label = "Filter by", sep = attr(choices, "sep"), drop_keys = FALSE )
filter_spec( vars, choices = NULL, selected = if (inherits(choices, "delayed_data")) NULL else choices[1], multiple = length(selected) > 1 || inherits(selected, "all_choices"), label = "Filter by", sep = attr(choices, "sep"), drop_keys = FALSE )
vars |
( |
choices |
( These shall be filter values of the
The
|
selected |
( |
multiple |
( |
label |
(optional |
sep |
( |
drop_keys |
(optional |
The filter_spec
is used inside teal
apps to allow filtering datasets
for their key variables. Imagine having an adverse events table. It has
the columns PARAMCD
and CNSR
. PARAMCD
contains the levels
"OS"
, "PFS"
, "EFS"
. CNSR
contains the levels "0"
and "1"
.
The first example should show how a filter_spec
setup will influence
the drop-down menu the app user will see.
filter_spec
-S3-class object or delayed_filter_spec
-S3-class object.
# for Adverse Events table filter_spec( vars = c("PARAMCD", "CNSR"), sep = "-", choices = c("OS-1" = "OS-1", "OS-0" = "OS-0", "PFS-1" = "PFS-1"), selected = "OS-1", multiple = FALSE, label = "Choose endpoint and Censor" ) # filtering a single variable filter_spec( vars = c("PARAMCD"), sep = "-", choices = c("OS", "PFS", "EFS"), selected = "OS", multiple = FALSE, label = "Choose endpoint" ) # filtering a single variable by multiple levels of the variable filter_spec( vars = c("PARAMCD"), sep = "-", choices = c("OS", "PFS", "EFS"), selected = c("OS", "PFS"), multiple = TRUE, label = "Choose endpoint" ) # delayed version filter_spec( vars = variable_choices("ADSL", "SEX"), sep = "-", choices = value_choices("ADSL", "SEX", "SEX"), selected = "F", multiple = FALSE, label = "Choose endpoint and Censor" ) # using `choices_selected()` filter_spec( vars = choices_selected(variable_choices("ADSL", subset = c("SEX", "AGE")), "SEX", fixed = FALSE), multiple = TRUE ) filter_spec( vars = choices_selected(variable_choices("ADSL"), "SEX", fixed = TRUE), multiple = TRUE ) # choose all choices adsl_filter <- filter_spec( vars = choices_selected(variable_choices("ADSL"), "SEX", fixed = FALSE), choices = value_choices("ADSL", "SEX"), selected = all_choices() )
# for Adverse Events table filter_spec( vars = c("PARAMCD", "CNSR"), sep = "-", choices = c("OS-1" = "OS-1", "OS-0" = "OS-0", "PFS-1" = "PFS-1"), selected = "OS-1", multiple = FALSE, label = "Choose endpoint and Censor" ) # filtering a single variable filter_spec( vars = c("PARAMCD"), sep = "-", choices = c("OS", "PFS", "EFS"), selected = "OS", multiple = FALSE, label = "Choose endpoint" ) # filtering a single variable by multiple levels of the variable filter_spec( vars = c("PARAMCD"), sep = "-", choices = c("OS", "PFS", "EFS"), selected = c("OS", "PFS"), multiple = TRUE, label = "Choose endpoint" ) # delayed version filter_spec( vars = variable_choices("ADSL", "SEX"), sep = "-", choices = value_choices("ADSL", "SEX", "SEX"), selected = "F", multiple = FALSE, label = "Choose endpoint and Censor" ) # using `choices_selected()` filter_spec( vars = choices_selected(variable_choices("ADSL", subset = c("SEX", "AGE")), "SEX", fixed = FALSE), multiple = TRUE ) filter_spec( vars = choices_selected(variable_choices("ADSL"), "SEX", fixed = TRUE), multiple = TRUE ) # choose all choices adsl_filter <- filter_spec( vars = choices_selected(variable_choices("ADSL"), "SEX", fixed = FALSE), choices = value_choices("ADSL", "SEX"), selected = all_choices() )
Returns a human-readable string representation of an extracted data_extract_spec
object.
format_data_extract(data_extract)
format_data_extract(data_extract)
data_extract |
|
This function formats the output of data_extract_srv
.
See the example for more information.
character(1)
representation of the data_extract
object.
library(shiny) simple_des <- data_extract_spec( dataname = "iris", filter = filter_spec(vars = "Petal.Length", choices = c("1.4", "1.5")), select = select_spec(choices = c("Petal.Length", "Species")) ) ui <- fluidPage( data_extract_ui( id = "extract", label = "data extract ui", data_extract_spec = simple_des, is_single_dataset = TRUE ), verbatimTextOutput("formatted_extract") ) server <- function(input, output, session) { extracted_input <- data_extract_srv( id = "extract", datasets = list(iris = iris), data_extract_spec = simple_des ) output$formatted_extract <- renderPrint({ cat(format_data_extract(extracted_input())) }) } if (interactive()) { shinyApp(ui, server) }
library(shiny) simple_des <- data_extract_spec( dataname = "iris", filter = filter_spec(vars = "Petal.Length", choices = c("1.4", "1.5")), select = select_spec(choices = c("Petal.Length", "Species")) ) ui <- fluidPage( data_extract_ui( id = "extract", label = "data extract ui", data_extract_spec = simple_des, is_single_dataset = TRUE ), verbatimTextOutput("formatted_extract") ) server <- function(input, output, session) { extracted_input <- data_extract_srv( id = "extract", datasets = list(iris = iris), data_extract_spec = simple_des ) output$formatted_extract <- renderPrint({ cat(format_data_extract(extracted_input())) }) } if (interactive()) { shinyApp(ui, server) }
get_anl_relabel_call(columns_source, datasets, anl_name = "ANL")
get_anl_relabel_call(columns_source, datasets, anl_name = "ANL")
columns_source |
(named |
datasets |
(named |
anl_name |
( |
(call
) to relabel dataset
and assign to anl_name
.
get_dataset_prefixed_col_names(data)
get_dataset_prefixed_col_names(data)
data |
( |
A named character
vector with the non-key columns of the data
.
data_extract_spec
objectsFetches dataname
slot per data_extract_spec
from a list of
data_extract_spec
.
get_extract_datanames(data_extracts)
get_extract_datanames(data_extracts)
data_extracts |
( |
character
vector with the unique dataname
set.
Creates list of calls depending on selector(s) and type of the merge. The merge order is the same as in selectors passed to the function.
get_merge_call( selector_list, join_keys = teal.data::join_keys(), dplyr_call_data = get_dplyr_call_data(selector_list, join_keys = join_keys), merge_function = "dplyr::full_join", anl_name = "ANL" )
get_merge_call( selector_list, join_keys = teal.data::join_keys(), dplyr_call_data = get_dplyr_call_data(selector_list, join_keys = join_keys), merge_function = "dplyr::full_join", anl_name = "ANL" )
selector_list |
( |
join_keys |
( |
dplyr_call_data |
( |
merge_function |
( |
anl_name |
( |
List with merge call
elements.
Function creates relabel call from named character.
get_relabel_call(labels)
get_relabel_call(labels)
labels |
(named |
call
object with relabel step.
get_relabel_call( labels = c( x = as.name("ANL"), AGE = "Age", AVAL = "Continuous variable" ) ) get_relabel_call( labels = c( AGE = "Age", AVAL = "Continuous variable" ) )
get_relabel_call( labels = c( x = as.name("ANL"), AGE = "Age", AVAL = "Continuous variable" ) ) get_relabel_call( labels = c( AGE = "Age", AVAL = "Continuous variable" ) )
Checks if the input data_extract_spec
objects all come from the same dataset.
is_single_dataset(...)
is_single_dataset(...)
... |
either |
TRUE
if all data_extract_spec
objects come from the same dataset,
FALSE
otherwise.
list_extract_spec(x, allow_null = FALSE)
list_extract_spec(x, allow_null = FALSE)
x |
( |
allow_null |
( |
x
as a list if it is not already.
Combines/merges multiple datasets with specified keys attribute.
merge_datasets( selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" )
merge_datasets( selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" )
selector_list |
( |
datasets |
(named |
join_keys |
( |
merge_function |
( |
anl_name |
( |
Internally this function uses calls to allow reproducibility.
This function is often used inside a teal
module server function with the
selectors
being the output of data_extract_srv
or data_extract_multiple_srv
.
# inside teal module server function response <- data_extract_srv( id = "reponse", data_extract_spec = response_spec, datasets = datasets ) regressor <- data_extract_srv( id = "regressor", data_extract_spec = regressor_spec, datasets = datasets ) merged_data <- merge_datasets(list(regressor(), response()))
merged_dataset
list containing:
expr
(list
of call
) code needed to replicate merged dataset;
columns_source
(list
) of column names selected for particular selector;
Each list element contains named character vector where:
Values are the names of the columns in the ANL
. In case if the same column name is selected in more than one
selector it gets prefixed by the id of the selector. For example if two data_extract
have id x
, y
, then
their duplicated selected variable (for example AGE
) is prefixed to be x.AGE
and y.AGE
;
Names of the vector denote names of the variables in the input dataset;
attr(,"dataname")
to indicate which dataset variable is merged from;
attr(, "always selected")
to denote the names of the variables which need to be always selected;
keys
(list
) the keys of the merged dataset;
filter_info
(list
) The information given by the user. This information
defines the filters that are applied on the data. Additionally it defines
the variables that are selected from the data sets.
library(shiny) library(teal.data) X <- data.frame(A = c(1, 1:3), B = 2:5, D = 1:4, E = letters[1:4], G = letters[6:9]) Y <- data.frame(A = c(1, 1, 2), B = 2:4, C = c(4, 4:5), E = letters[4:6], G = letters[1:3]) join_keys <- join_keys(join_key("X", "Y", c("A", "B"))) selector_list <- list( list( dataname = "X", filters = NULL, select = "E", keys = c("A", "B"), reshape = FALSE, internal_id = "x" ), list( dataname = "Y", filters = NULL, select = "G", keys = c("A", "C"), reshape = FALSE, internal_id = "y" ) ) data_list <- list(X = reactive(X), Y = reactive(Y)) merged_datasets <- isolate( merge_datasets( selector_list = selector_list, datasets = data_list, join_keys = join_keys ) ) paste(merged_datasets$expr)
library(shiny) library(teal.data) X <- data.frame(A = c(1, 1:3), B = 2:5, D = 1:4, E = letters[1:4], G = letters[6:9]) Y <- data.frame(A = c(1, 1, 2), B = 2:4, C = c(4, 4:5), E = letters[4:6], G = letters[1:3]) join_keys <- join_keys(join_key("X", "Y", c("A", "B"))) selector_list <- list( list( dataname = "X", filters = NULL, select = "E", keys = c("A", "B"), reshape = FALSE, internal_id = "x" ), list( dataname = "Y", filters = NULL, select = "G", keys = c("A", "C"), reshape = FALSE, internal_id = "y" ) ) data_list <- list(X = reactive(X), Y = reactive(Y)) merged_datasets <- isolate( merge_datasets( selector_list = selector_list, datasets = data_list, join_keys = join_keys ) ) paste(merged_datasets$expr)
Convenient wrapper to combine data_extract_multiple_srv()
and
merge_expression_srv()
when no additional processing is required.
Compare the example below with that found in merge_expression_srv()
.
merge_expression_module( datasets, join_keys = NULL, data_extract, merge_function = "dplyr::full_join", anl_name = "ANL", id = "merge_id" ) ## S3 method for class 'reactive' merge_expression_module( datasets, join_keys = NULL, data_extract, merge_function = "dplyr::full_join", anl_name = "ANL", id = "merge_id" ) ## S3 method for class 'list' merge_expression_module( datasets, join_keys = NULL, data_extract, merge_function = "dplyr::full_join", anl_name = "ANL", id = "merge_id" )
merge_expression_module( datasets, join_keys = NULL, data_extract, merge_function = "dplyr::full_join", anl_name = "ANL", id = "merge_id" ) ## S3 method for class 'reactive' merge_expression_module( datasets, join_keys = NULL, data_extract, merge_function = "dplyr::full_join", anl_name = "ANL", id = "merge_id" ) ## S3 method for class 'list' merge_expression_module( datasets, join_keys = NULL, data_extract, merge_function = "dplyr::full_join", anl_name = "ANL", id = "merge_id" )
datasets |
(named |
join_keys |
( |
data_extract |
(named |
merge_function |
( |
anl_name |
( |
id |
An ID string that corresponds with the ID used to call the module's UI function. |
Reactive expression with output from merge_expression_srv()
.
library(shiny) library(teal.data) library(teal.widgets) ADSL <- data.frame( STUDYID = "A", USUBJID = LETTERS[1:10], SEX = rep(c("F", "M"), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) ) ADLB <- expand.grid( STUDYID = "A", USUBJID = LETTERS[1:10], PARAMCD = c("ALT", "CRP", "IGA"), AVISIT = c("SCREENING", "BASELINE", "WEEK 1 DAY 8", "WEEK 2 DAY 15") ) ADLB$AVAL <- rlnorm(120) ADLB$CHG <- rnorm(120) data_list <- list( ADSL = reactive(ADSL), ADLB = reactive(ADLB) ) join_keys <- join_keys( join_key("ADSL", "ADSL", c("STUDYID", "USUBJID")), join_key("ADSL", "ADLB", c("STUDYID", "USUBJID")), join_key("ADLB", "ADLB", c("STUDYID", "USUBJID", "PARAMCD", "AVISIT")) ) adsl_extract <- data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = c("AGE", "BMRKR1"), selected = "AGE", multiple = TRUE, fixed = FALSE ) ) adlb_extract <- data_extract_spec( dataname = "ADLB", filter = filter_spec(vars = "PARAMCD", choices = c("ALT", "CRP", "IGA"), selected = "ALT"), select = select_spec( label = "Select variable:", choices = c("AVAL", "CHG"), selected = "AVAL", multiple = TRUE, fixed = FALSE ) ) ui <- fluidPage( standard_layout( output = div( verbatimTextOutput("expr"), dataTableOutput("data") ), encoding = tagList( data_extract_ui("adsl_var", label = "ADSL selection", adsl_extract), data_extract_ui("adlb_var", label = "ADLB selection", adlb_extract) ) ) ) server <- function(input, output, session) { data_q <- qenv() data_q <- eval_code( data_q, "ADSL <- data.frame( STUDYID = 'A', USUBJID = LETTERS[1:10], SEX = rep(c('F', 'M'), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) )" ) data_q <- eval_code( data_q, "ADLB <- expand.grid( STUDYID = 'A', USUBJID = LETTERS[1:10], PARAMCD = c('ALT', 'CRP', 'IGA'), AVISIT = c('SCREENING', 'BASELINE', 'WEEK 1 DAY 8', 'WEEK 2 DAY 15'), AVAL = rlnorm(120), CHG = rlnorm(120) )" ) merged_data <- merge_expression_module( data_extract = list(adsl_var = adsl_extract, adlb_var = adlb_extract), datasets = data_list, join_keys = join_keys, merge_function = "dplyr::left_join" ) code_merge <- reactive({ for (exp in merged_data()$expr) data_q <- eval_code(data_q, exp) data_q }) output$expr <- renderText(paste(merged_data()$expr, collapse = "\n")) output$data <- renderDataTable(code_merge()[["ANL"]]) } if (interactive()) { shinyApp(ui, server) }
library(shiny) library(teal.data) library(teal.widgets) ADSL <- data.frame( STUDYID = "A", USUBJID = LETTERS[1:10], SEX = rep(c("F", "M"), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) ) ADLB <- expand.grid( STUDYID = "A", USUBJID = LETTERS[1:10], PARAMCD = c("ALT", "CRP", "IGA"), AVISIT = c("SCREENING", "BASELINE", "WEEK 1 DAY 8", "WEEK 2 DAY 15") ) ADLB$AVAL <- rlnorm(120) ADLB$CHG <- rnorm(120) data_list <- list( ADSL = reactive(ADSL), ADLB = reactive(ADLB) ) join_keys <- join_keys( join_key("ADSL", "ADSL", c("STUDYID", "USUBJID")), join_key("ADSL", "ADLB", c("STUDYID", "USUBJID")), join_key("ADLB", "ADLB", c("STUDYID", "USUBJID", "PARAMCD", "AVISIT")) ) adsl_extract <- data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = c("AGE", "BMRKR1"), selected = "AGE", multiple = TRUE, fixed = FALSE ) ) adlb_extract <- data_extract_spec( dataname = "ADLB", filter = filter_spec(vars = "PARAMCD", choices = c("ALT", "CRP", "IGA"), selected = "ALT"), select = select_spec( label = "Select variable:", choices = c("AVAL", "CHG"), selected = "AVAL", multiple = TRUE, fixed = FALSE ) ) ui <- fluidPage( standard_layout( output = div( verbatimTextOutput("expr"), dataTableOutput("data") ), encoding = tagList( data_extract_ui("adsl_var", label = "ADSL selection", adsl_extract), data_extract_ui("adlb_var", label = "ADLB selection", adlb_extract) ) ) ) server <- function(input, output, session) { data_q <- qenv() data_q <- eval_code( data_q, "ADSL <- data.frame( STUDYID = 'A', USUBJID = LETTERS[1:10], SEX = rep(c('F', 'M'), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) )" ) data_q <- eval_code( data_q, "ADLB <- expand.grid( STUDYID = 'A', USUBJID = LETTERS[1:10], PARAMCD = c('ALT', 'CRP', 'IGA'), AVISIT = c('SCREENING', 'BASELINE', 'WEEK 1 DAY 8', 'WEEK 2 DAY 15'), AVAL = rlnorm(120), CHG = rlnorm(120) )" ) merged_data <- merge_expression_module( data_extract = list(adsl_var = adsl_extract, adlb_var = adlb_extract), datasets = data_list, join_keys = join_keys, merge_function = "dplyr::left_join" ) code_merge <- reactive({ for (exp in merged_data()$expr) data_q <- eval_code(data_q, exp) data_q }) output$expr <- renderText(paste(merged_data()$expr, collapse = "\n")) output$data <- renderDataTable(code_merge()[["ANL"]]) } if (interactive()) { shinyApp(ui, server) }
merge_expression_srv( id = "merge_id", selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" ) ## S3 method for class 'reactive' merge_expression_srv( id = "merge_id", selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" ) ## S3 method for class 'list' merge_expression_srv( id = "merge_id", selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" )
merge_expression_srv( id = "merge_id", selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" ) ## S3 method for class 'reactive' merge_expression_srv( id = "merge_id", selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" ) ## S3 method for class 'list' merge_expression_srv( id = "merge_id", selector_list, datasets, join_keys, merge_function = "dplyr::full_join", anl_name = "ANL" )
id |
An ID string that corresponds with the ID used to call the module's UI function. |
selector_list |
( |
datasets |
(named |
join_keys |
( |
merge_function |
( |
anl_name |
( |
When additional processing of the data_extract
list input is required,
merge_expression_srv()
can be combined with data_extract_multiple_srv()
or data_extract_srv()
to influence the selector_list
input.
Compare the example below with that found in merge_expression_module()
.
Reactive expression with output from merge_expression_srv()
.
library(shiny) library(teal.data) library(teal.widgets) ADSL <- data.frame( STUDYID = "A", USUBJID = LETTERS[1:10], SEX = rep(c("F", "M"), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) ) ADLB <- expand.grid( STUDYID = "A", USUBJID = LETTERS[1:10], PARAMCD = c("ALT", "CRP", "IGA"), AVISIT = c("SCREENING", "BASELINE", "WEEK 1 DAY 8", "WEEK 2 DAY 15") ) ADLB$AVAL <- rlnorm(120) ADLB$CHG <- rlnorm(120) data_list <- list( ADSL = reactive(ADSL), ADLB = reactive(ADLB) ) join_keys <- join_keys( join_key("ADSL", "ADSL", c("STUDYID", "USUBJID")), join_key("ADSL", "ADLB", c("STUDYID", "USUBJID")), join_key("ADLB", "ADLB", c("STUDYID", "USUBJID", "PARAMCD", "AVISIT")) ) adsl_extract <- data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = c("AGE", "BMRKR1"), selected = "AGE", multiple = TRUE, fixed = FALSE ) ) adlb_extract <- data_extract_spec( dataname = "ADLB", filter = filter_spec(vars = "PARAMCD", choices = c("ALT", "CRP", "IGA"), selected = "ALT"), select = select_spec( label = "Select variable:", choices = c("AVAL", "CHG"), selected = "AVAL", multiple = TRUE, fixed = FALSE ) ) ui <- fluidPage( standard_layout( output = div( verbatimTextOutput("expr"), dataTableOutput("data") ), encoding = tagList( data_extract_ui("adsl_var", label = "ADSL selection", adsl_extract), data_extract_ui("adlb_var", label = "ADLB selection", adlb_extract) ) ) ) server <- function(input, output, session) { data_q <- qenv() data_q <- eval_code( data_q, "ADSL <- data.frame( STUDYID = 'A', USUBJID = LETTERS[1:10], SEX = rep(c('F', 'M'), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) )" ) data_q <- eval_code( data_q, "ADLB <- expand.grid( STUDYID = 'A', USUBJID = LETTERS[1:10], PARAMCD = c('ALT', 'CRP', 'IGA'), AVISIT = c('SCREENING', 'BASELINE', 'WEEK 1 DAY 8', 'WEEK 2 DAY 15'), AVAL = rlnorm(120), CHG = rlnorm(120) )" ) selector_list <- data_extract_multiple_srv( list(adsl_var = adsl_extract, adlb_var = adlb_extract), datasets = data_list ) merged_data <- merge_expression_srv( selector_list = selector_list, datasets = data_list, join_keys = join_keys, merge_function = "dplyr::left_join" ) code_merge <- reactive({ for (exp in merged_data()$expr) data_q <- eval_code(data_q, exp) data_q }) output$expr <- renderText(paste(merged_data()$expr, collapse = "\n")) output$data <- renderDataTable(code_merge()[["ANL"]]) } if (interactive()) { shinyApp(ui, server) }
library(shiny) library(teal.data) library(teal.widgets) ADSL <- data.frame( STUDYID = "A", USUBJID = LETTERS[1:10], SEX = rep(c("F", "M"), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) ) ADLB <- expand.grid( STUDYID = "A", USUBJID = LETTERS[1:10], PARAMCD = c("ALT", "CRP", "IGA"), AVISIT = c("SCREENING", "BASELINE", "WEEK 1 DAY 8", "WEEK 2 DAY 15") ) ADLB$AVAL <- rlnorm(120) ADLB$CHG <- rlnorm(120) data_list <- list( ADSL = reactive(ADSL), ADLB = reactive(ADLB) ) join_keys <- join_keys( join_key("ADSL", "ADSL", c("STUDYID", "USUBJID")), join_key("ADSL", "ADLB", c("STUDYID", "USUBJID")), join_key("ADLB", "ADLB", c("STUDYID", "USUBJID", "PARAMCD", "AVISIT")) ) adsl_extract <- data_extract_spec( dataname = "ADSL", select = select_spec( label = "Select variable:", choices = c("AGE", "BMRKR1"), selected = "AGE", multiple = TRUE, fixed = FALSE ) ) adlb_extract <- data_extract_spec( dataname = "ADLB", filter = filter_spec(vars = "PARAMCD", choices = c("ALT", "CRP", "IGA"), selected = "ALT"), select = select_spec( label = "Select variable:", choices = c("AVAL", "CHG"), selected = "AVAL", multiple = TRUE, fixed = FALSE ) ) ui <- fluidPage( standard_layout( output = div( verbatimTextOutput("expr"), dataTableOutput("data") ), encoding = tagList( data_extract_ui("adsl_var", label = "ADSL selection", adsl_extract), data_extract_ui("adlb_var", label = "ADLB selection", adlb_extract) ) ) ) server <- function(input, output, session) { data_q <- qenv() data_q <- eval_code( data_q, "ADSL <- data.frame( STUDYID = 'A', USUBJID = LETTERS[1:10], SEX = rep(c('F', 'M'), 5), AGE = rpois(10, 30), BMRKR1 = rlnorm(10) )" ) data_q <- eval_code( data_q, "ADLB <- expand.grid( STUDYID = 'A', USUBJID = LETTERS[1:10], PARAMCD = c('ALT', 'CRP', 'IGA'), AVISIT = c('SCREENING', 'BASELINE', 'WEEK 1 DAY 8', 'WEEK 2 DAY 15'), AVAL = rlnorm(120), CHG = rlnorm(120) )" ) selector_list <- data_extract_multiple_srv( list(adsl_var = adsl_extract, adlb_var = adlb_extract), datasets = data_list ) merged_data <- merge_expression_srv( selector_list = selector_list, datasets = data_list, join_keys = join_keys, merge_function = "dplyr::left_join" ) code_merge <- reactive({ for (exp in merged_data()$expr) data_q <- eval_code(data_q, exp) data_q }) output$expr <- renderText(paste(merged_data()$expr, collapse = "\n")) output$data <- renderDataTable(code_merge()[["ANL"]]) } if (interactive()) { shinyApp(ui, server) }
no_selected_as_NULL(x)
no_selected_as_NULL(x)
x |
( |
The word or NULL
.
resolve_delayed(x, datasets, keys) ## S3 method for class 'FilteredData' resolve_delayed( x, datasets, keys = sapply(datasets$datanames(), datasets$get_keys, simplify = FALSE) ) ## S3 method for class 'list' resolve_delayed(x, datasets, keys = NULL)
resolve_delayed(x, datasets, keys) ## S3 method for class 'FilteredData' resolve_delayed( x, datasets, keys = sapply(datasets$datanames(), datasets$get_keys, simplify = FALSE) ) ## S3 method for class 'list' resolve_delayed(x, datasets, keys = NULL)
x |
( |
datasets |
( |
keys |
(named |
Resolved object.
resolve_delayed(FilteredData)
: Default values for keys
parameters is extracted from datasets
.
resolve_delayed(list)
: Generic method when datasets
argument is a named list.
library(shiny) ADSL <- teal.transform::rADSL isolate({ data_list <- list(ADSL = reactive(ADSL)) # value_choices example v1 <- value_choices("ADSL", "SEX", "SEX") v1 resolve_delayed(v1, data_list) # variable_choices example v2 <- variable_choices("ADSL", c("BMRKR1", "BMRKR2")) v2 resolve_delayed(v2, data_list) # data_extract_spec example adsl_filter <- filter_spec( vars = variable_choices("ADSL", "SEX"), sep = "-", choices = value_choices("ADSL", "SEX", "SEX"), selected = "F", multiple = FALSE, label = "Choose endpoint and Censor" ) adsl_select <- select_spec( label = "Select variable:", choices = variable_choices("ADSL", c("BMRKR1", "BMRKR2")), selected = "BMRKR1", multiple = FALSE, fixed = FALSE ) adsl_de <- data_extract_spec( dataname = "ADSL", select = adsl_select, filter = adsl_filter ) resolve_delayed(adsl_filter, datasets = data_list) resolve_delayed(adsl_select, datasets = data_list) resolve_delayed(adsl_de, datasets = data_list) # nested list (arm_ref_comp) arm_ref_comp <- list( ARMCD = list( ref = variable_choices("ADSL"), comp = variable_choices("ADSL") ) ) resolve_delayed(arm_ref_comp, datasets = data_list) })
library(shiny) ADSL <- teal.transform::rADSL isolate({ data_list <- list(ADSL = reactive(ADSL)) # value_choices example v1 <- value_choices("ADSL", "SEX", "SEX") v1 resolve_delayed(v1, data_list) # variable_choices example v2 <- variable_choices("ADSL", c("BMRKR1", "BMRKR2")) v2 resolve_delayed(v2, data_list) # data_extract_spec example adsl_filter <- filter_spec( vars = variable_choices("ADSL", "SEX"), sep = "-", choices = value_choices("ADSL", "SEX", "SEX"), selected = "F", multiple = FALSE, label = "Choose endpoint and Censor" ) adsl_select <- select_spec( label = "Select variable:", choices = variable_choices("ADSL", c("BMRKR1", "BMRKR2")), selected = "BMRKR1", multiple = FALSE, fixed = FALSE ) adsl_de <- data_extract_spec( dataname = "ADSL", select = adsl_select, filter = adsl_filter ) resolve_delayed(adsl_filter, datasets = data_list) resolve_delayed(adsl_select, datasets = data_list) resolve_delayed(adsl_de, datasets = data_list) # nested list (arm_ref_comp) arm_ref_comp <- list( ARMCD = list( ref = variable_choices("ADSL"), comp = variable_choices("ADSL") ) ) resolve_delayed(arm_ref_comp, datasets = data_list) })
select_spec
is used inside teal
to create a shiny::selectInput()
that will select columns from a dataset.
select_spec( choices, selected = if (inherits(choices, "delayed_data")) NULL else choices[1], multiple = length(selected) > 1 || inherits(selected, "all_choices"), fixed = FALSE, always_selected = NULL, ordered = FALSE, label = "Select" ) select_spec.delayed_data( choices, selected = NULL, multiple = length(selected) > 1, fixed = FALSE, always_selected = NULL, ordered = FALSE, label = NULL ) select_spec.default( choices, selected = choices[1], multiple = length(selected) > 1, fixed = FALSE, always_selected = NULL, ordered = FALSE, label = NULL )
select_spec( choices, selected = if (inherits(choices, "delayed_data")) NULL else choices[1], multiple = length(selected) > 1 || inherits(selected, "all_choices"), fixed = FALSE, always_selected = NULL, ordered = FALSE, label = "Select" ) select_spec.delayed_data( choices, selected = NULL, multiple = length(selected) > 1, fixed = FALSE, always_selected = NULL, ordered = FALSE, label = NULL ) select_spec.default( choices, selected = choices[1], multiple = length(selected) > 1, fixed = FALSE, always_selected = NULL, ordered = FALSE, label = NULL )
choices |
( |
selected |
(optional |
multiple |
( |
fixed |
(optional |
always_selected |
( |
ordered |
( |
label |
(optional |
A select_spec
-S3 class object or delayed_select_spec
-S3-class object.
It contains all input values.
If select_spec
, then the function double checks the choices
and selected
inputs.
# Selection with just one column allowed select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL"), multiple = FALSE, fixed = FALSE, label = "Column" ) # Selection with just multiple columns allowed select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = FALSE, label = "Columns" ) # Selection without user access select_spec( choices = c("AVAL", "BMRKR1"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = TRUE, label = "Columns" ) # Delayed version select_spec( label = "Select variable:", choices = variable_choices("ADSL", c("BMRKR1", "BMRKR2")), selected = "BMRKR1", multiple = FALSE, fixed = FALSE ) # all_choices passed to selected select_spec( label = "Select variable:", choices = variable_choices("ADSL", c("BMRKR1", "BMRKR2")), selected = all_choices() ) # Both below objects are semantically the same select_spec(choices = variable_choices("ADSL"), selected = variable_choices("ADSL")) select_spec(choices = variable_choices("ADSL"), selected = all_choices())
# Selection with just one column allowed select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL"), multiple = FALSE, fixed = FALSE, label = "Column" ) # Selection with just multiple columns allowed select_spec( choices = c("AVAL", "BMRKR1", "AGE"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = FALSE, label = "Columns" ) # Selection without user access select_spec( choices = c("AVAL", "BMRKR1"), selected = c("AVAL", "BMRKR1"), multiple = TRUE, fixed = TRUE, label = "Columns" ) # Delayed version select_spec( label = "Select variable:", choices = variable_choices("ADSL", c("BMRKR1", "BMRKR2")), selected = "BMRKR1", multiple = FALSE, fixed = FALSE ) # all_choices passed to selected select_spec( label = "Select variable:", choices = variable_choices("ADSL", c("BMRKR1", "BMRKR2")), selected = all_choices() ) # Both below objects are semantically the same select_spec(choices = variable_choices("ADSL"), selected = variable_choices("ADSL")) select_spec(choices = variable_choices("ADSL"), selected = all_choices())
split_by_sep(x, sep)
split_by_sep(x, sep)
x |
( |
sep |
( |
List of character vectors split by sep
. Self if x
is not a character
.
Wrapper on choices_labeled to label variable values basing on other variable values.
value_choices(data, var_choices, var_label = NULL, subset = NULL, sep = " - ") ## S3 method for class 'character' value_choices(data, var_choices, var_label = NULL, subset = NULL, sep = " - ") ## S3 method for class 'data.frame' value_choices(data, var_choices, var_label = NULL, subset = NULL, sep = " - ")
value_choices(data, var_choices, var_label = NULL, subset = NULL, sep = " - ") ## S3 method for class 'character' value_choices(data, var_choices, var_label = NULL, subset = NULL, sep = " - ") ## S3 method for class 'data.frame' value_choices(data, var_choices, var_label = NULL, subset = NULL, sep = " - ")
data |
( |
var_choices |
( |
var_label |
( |
subset |
( See examples for more details. |
sep |
( |
named character vector or delayed_data
object.
ADRS <- teal.transform::rADRS value_choices(ADRS, "PARAMCD", "PARAM", subset = c("BESRSPI", "INVET")) value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM")) value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM"), subset = c("BESRSPI - ARM A", "INVET - ARM A", "OVRINV - ARM A") ) value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM"), sep = " --- ") # delayed version value_choices("ADRS", c("PARAMCD", "ARMCD"), c("PARAM", "ARM")) # functional subset value_choices(ADRS, "PARAMCD", "PARAM", subset = function(data) { levels(data$PARAMCD)[1:2] })
ADRS <- teal.transform::rADRS value_choices(ADRS, "PARAMCD", "PARAM", subset = c("BESRSPI", "INVET")) value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM")) value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM"), subset = c("BESRSPI - ARM A", "INVET - ARM A", "OVRINV - ARM A") ) value_choices(ADRS, c("PARAMCD", "ARMCD"), c("PARAM", "ARM"), sep = " --- ") # delayed version value_choices("ADRS", c("PARAMCD", "ARMCD"), c("PARAM", "ARM")) # functional subset value_choices(ADRS, "PARAMCD", "PARAM", subset = function(data) { levels(data$PARAMCD)[1:2] })
Wrapper on choices_labeled to label variables basing on existing labels in data.
variable_choices(data, subset = NULL, fill = FALSE, key = NULL) ## S3 method for class 'character' variable_choices(data, subset = NULL, fill = FALSE, key = NULL) ## S3 method for class 'data.frame' variable_choices(data, subset = NULL, fill = TRUE, key = NULL)
variable_choices(data, subset = NULL, fill = FALSE, key = NULL) ## S3 method for class 'character' variable_choices(data, subset = NULL, fill = FALSE, key = NULL) ## S3 method for class 'data.frame' variable_choices(data, subset = NULL, fill = TRUE, key = NULL)
data |
( |
subset |
( See examples for more details. |
fill |
( |
key |
( This is an optional argument, which allows to identify variables associated
with the primary key and display the appropriate icon for them in the
|
Named character
vector with additional attributes or delayed_data
object.
library(teal.data) ADRS <- teal.transform::rADRS variable_choices(ADRS) variable_choices(ADRS, subset = c("PARAM", "PARAMCD")) variable_choices(ADRS, subset = c("", "PARAM", "PARAMCD")) variable_choices( ADRS, subset = c("", "PARAM", "PARAMCD"), key = default_cdisc_join_keys["ADRS", "ADRS"] ) # delayed version variable_choices("ADRS", subset = c("USUBJID", "STUDYID")) # functional subset (with delayed data) - return only factor variables variable_choices("ADRS", subset = function(data) { idx <- vapply(data, is.factor, logical(1)) names(data)[idx] })
library(teal.data) ADRS <- teal.transform::rADRS variable_choices(ADRS) variable_choices(ADRS, subset = c("PARAM", "PARAMCD")) variable_choices(ADRS, subset = c("", "PARAM", "PARAMCD")) variable_choices( ADRS, subset = c("", "PARAM", "PARAMCD"), key = default_cdisc_join_keys["ADRS", "ADRS"] ) # delayed version variable_choices("ADRS", subset = c("USUBJID", "STUDYID")) # functional subset (with delayed data) - return only factor variables variable_choices("ADRS", subset = function(data) { idx <- vapply(data, is.factor, logical(1)) names(data)[idx] })