Skip to contents

[Stable] It consists in choices and additionally the variable names for the choices

Usage

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
)

Arguments

vars

(character) or (delayed_data) object. Character vector giving the columns to be filtered. These should be key variables of the data set to be filtered. delayed_data objects can be created via variable_choices(), value_choices(), or choices_selected.

choices

(character or numeric or logical or (delayed_data) object. Named character vector to define the choices of a shiny selectInput. These choices will be used to filter the dataset.

These shall be filter values of the vars input separated by the separator(sep). Please watch out that the filter values have to follow the order of the vars input. In the following example we will show how to filter two columns:

vars = c("PARAMCD","AVISIT") and choices = c("CRP - BASELINE", "ALT - BASELINE") will lead to a filtering of (PARAMCD == "CRP" & AVISIT == "BASELINE") | (PARAMCD == "ALT" & AVISIT == "BASELINE").

The sep input has to be " - " in this case.

delayed_data objects can be created via variable_choices or value_choices.

selected

(character or numeric or logical or (delayed_data or (all_choices)) object. Named character vector to define the selected values of a shiny selectInput (default values). This value will be displayed inside the shiny app upon start. The all_choices object indicates selecting all possible choices.

multiple

(logical) Whether multiple values shall be allowed in the shiny selectInput.

label

optional (character). Define a label on top of this specific shiny selectInput. The default value is "Filter by".

sep

(character) A separator string to split the choices or selected inputs into the values of the different columns

drop_keys

optional, (logical) whether to drop filter column from the dataset keys, TRUE on default.

Value

filter_spec-S3-class object or delayed_filter_spec-S3-class object.

Details

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 following examples should show how a filter_spec setup will influence the drop-down menu the app user will see:

  1. Filtering two variables

    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 two variablesFiltering two variables
  2. Filtering a single variable

    filter_spec(
           vars = c("PARAMCD"),
           sep = "-",
           choices = c("OS", "PFS", "EFS"),
           selected = "OS",
           multiple = FALSE,
           label = "Choose endpoint"
         )
    Filtering two variablesFiltering two variables
  3. 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"
         )
    Filtering two variablesFiltering two variables
  4. Delayed version

    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"
          )
  5. Using choices_selected

    
          adsl_filter <- filter_spec(
            vars = choices_selected(variable_choices("ADSL", subset = c("SEX", "AGE")), "SEX", fixed = FALSE),
            multiple = TRUE
          )      adsl_filter2 <- filter_spec(
            vars = choices_selected(variable_choices("ADSL"), "SEX", fixed = TRUE),
            multiple = TRUE
          )
        
  6. Choose all choices

    adsl_filter <- filter_spec(
            vars = choices_selected(variable_choices("ADSL"), "SEX", fixed = FALSE),
            choices = value_choices("ADSL", "SEX"),
            selected = all_choices()
          )

Examples

filter_spec(
  vars = c("PARAMCD", "AVISIT"),
  sep = " - ",
  choices = c("CRP - BASELINE", "CRP - SCREENING", "ALT - BASELINE"),
  selected = c("CRP - BASELINE"),
  multiple = TRUE
)
#> filter_spec with delayed data:
#> $ vars_choices
#> [1] "PARAMCD" "AVISIT" 
#> $ vars_selected
#> [1] "PARAMCD" "AVISIT" 
#> $ vars_label
#> NULL
#> $ vars_fixed
#> [1] TRUE
#> $ vars_multiple
#> [1] TRUE
#> $ choices
#> [1] "CRP - BASELINE"  "CRP - SCREENING" "ALT - BASELINE" 
#> $ selected
#> [1] "CRP - BASELINE"
#> $ label
#> [1] "Filter by"
#> $ multiple
#> [1] TRUE
#> $ fixed
#> [1] FALSE
#> $ sep
#> [1] " - "
#> $ drop_keys
#> [1] FALSE
#> $ dataname
#> NULL
#> $ initialized
#> [1] FALSE

filter_spec(
  vars = c("PARAMCD"),
  sep = " - ",
  choices = c("CRP", "ALT"),
  selected = c("CRP"),
  multiple = TRUE
)
#> filter_spec with delayed data:
#> $ vars_choices
#> [1] "PARAMCD"
#> $ vars_selected
#> [1] "PARAMCD"
#> $ vars_label
#> NULL
#> $ vars_fixed
#> [1] TRUE
#> $ vars_multiple
#> [1] TRUE
#> $ choices
#> [1] "CRP" "ALT"
#> $ selected
#> [1] "CRP"
#> $ label
#> [1] "Filter by"
#> $ multiple
#> [1] TRUE
#> $ fixed
#> [1] FALSE
#> $ sep
#> [1] " - "
#> $ drop_keys
#> [1] FALSE
#> $ dataname
#> NULL
#> $ initialized
#> [1] FALSE
filter_spec(
  vars = variable_choices("ADSL", "ARMCD"),
  choices = value_choices("ADSL",
    var_choices = "ARMCD", var_label = "ARM",
    subset = function(data) {
      levels(data$ARMCD)[1:2]
    }
  ),
  selected = value_choices("ADSL",
    var_choices = "ARMCD", var_label = "ARM",
    subset = function(data) {
      levels(data$ARMCD)[1]
    }
  )
)
#> filter_spec with delayed data:
#> $ vars_choices
#>   variable_choices with delayed data: ADSL
#>   $ data
#>   [1] "ADSL"
#>   $ subset
#>   [1] "ARMCD"
#>   $ key
#>   NULL
#> $ vars_selected
#>   variable_choices with delayed data: ADSL
#>   $ data
#>   [1] "ADSL"
#>   $ subset
#>   [1] "ARMCD"
#>   $ key
#>   NULL
#> $ vars_label
#> NULL
#> $ vars_fixed
#> [1] TRUE
#> $ vars_multiple
#> [1] TRUE
#> $ choices
#>   value_choices with delayed data:  ADSL
#>   $ data
#>   [1] "ADSL"
#>   $ var_choices
#>   [1] "ARMCD"
#>   $ var_label
#>   [1] "ARM"
#>   $ subset
#>   function(data) {
#>       levels(data$ARMCD)[1:2]
#>     }
#> <environment: 0x562f62e08688>
#>   $ sep
#>   [1] " - "
#> $ selected
#>   value_choices with delayed data:  ADSL
#>   $ data
#>   [1] "ADSL"
#>   $ var_choices
#>   [1] "ARMCD"
#>   $ var_label
#>   [1] "ARM"
#>   $ subset
#>   function(data) {
#>       levels(data$ARMCD)[1]
#>     }
#> <environment: 0x562f62e08688>
#>   $ sep
#>   [1] " - "
#> $ label
#> [1] "Filter by"
#> $ multiple
#> [1] TRUE
#> $ fixed
#> [1] FALSE
#> $ sep
#> [1] " - "
#> $ drop_keys
#> [1] FALSE
#> $ dataname
#> NULL
#> $ initialized
#> [1] FALSE