Skip to contents

Class to encapsulate filtered datasets

Class to encapsulate filtered datasets

Details

The main purpose of this class is to provide a collection of reactive datasets, each dataset having a filter state that determines how it is filtered.

For each dataset, get_filter_expr returns the call to filter the dataset according to the filter state. The data itself can be obtained through get_data.

The datasets are filtered lazily, i.e. only when requested / needed in a Shiny app.

By design, any dataname set through set_dataset cannot be removed because other code may already depend on it. As a workaround, the underlying data can be set to NULL.

The class currently supports variables of the following types within datasets:

  • choices: variable of type factor, e.g. ADSL$COUNTRY, iris$Species zero or more options can be selected, when the variable is a factor

  • logical: variable of type logical, e.g. ADSL$TRT_FLAG exactly one option must be selected, TRUE or FALSE

  • ranges: variable of type numeric, e.g. ADSL$AGE, iris$Sepal.Length numerical range, a range within this range can be selected

  • dates: variable of type Date, POSIXlt Other variables cannot be used for filtering the data in this class.

Common arguments are:

  1. filtered: whether to return a filtered result or not

  2. dataname: the name of one of the datasets in this FilteredData

  3. varname: one of the columns in a dataset

Methods


Method new()

Initialize a FilteredData object

Usage

FilteredData$new(
  data_objects,
  join_keys = teal.data::join_keys(),
  code = NULL,
  check = FALSE
)

Arguments

data_objects

(list) should named elements containing data.frame or MultiAssayExperiment. Names of the list will serve as dataname.

join_keys

(JoinKeys or NULL) see teal.data::join_keys().

code

(CodeClass or NULL) see teal.data::CodeClass.

check

(logical(1)) whether data has been check against reproducibility.


Method datanames()

Gets datanames

The datanames are returned in the order in which they must be evaluated (in case of dependencies).

Usage

FilteredData$datanames()

Returns

(character vector) of datanames Gets data label for the dataset

Useful to display in Show R Code.


Method get_datalabel()

Usage

FilteredData$get_datalabel(dataname)

Arguments

dataname

(character(1)) name of the dataset

Returns

(character) keys of dataset Set list of external filter states available for activation.

Unlike adding new filter from the column, these filters can come with some prespecified settings. teal_slices are wrapped in a reactive so they can be updated from elsewhere in the app. Filters passed in x are limited to those that can be set for this FilteredData, i.e. they have the correct dataname and varname (waived teal_slice_fixed as they do not have varname). List is accessible in ui/srv_active through ui/srv_available_filters.


Method set_available_teal_slices()

Usage

FilteredData$set_available_teal_slices(x)

Arguments

x

(reactive)
should return teal_slices

Returns

invisible NULL Get list of filter states available for this object.

All teal_slice objects that have been created since the beginning of the app session are stored in one teal_slices object. This returns a subset of that teal_slices, describing filter states that can be set for this object.


Method get_available_teal_slices()

Usage

FilteredData$get_available_teal_slices()

Returns

reactive that returns teal_slices


Method get_call()

Gets a call to filter the dataset according to the filter state.

It returns a call to filter the dataset only, assuming the other (filtered) datasets it depends on are available.

Together with self$datanames() which returns the datasets in the correct evaluation order, this generates the whole filter code, see the function FilteredData$get_filter_code.

For the return type, note that rlang::is_expression returns TRUE on the return type, both for base R expressions and calls (single expression, capturing a function call).

The filtered dataset has the name given by self$filtered_dataname(dataname)

This can be used for the Show R Code generation.

Usage

FilteredData$get_call(dataname)

Arguments

dataname

(character(1)) name of the dataset

Returns

(call or list of calls) to filter dataset calls


Method get_code()

Gets the R preprocessing code string that generates the unfiltered datasets.

Usage

FilteredData$get_code(dataname = self$datanames())

Arguments

dataname

(character(1)) name(s) of dataset(s)

Returns

(character(1)) deparsed code


Method get_data()

Gets filtered or unfiltered dataset.

For filtered = FALSE, the original data set with set_data is returned including all attributes.

Usage

FilteredData$get_data(dataname, filtered = TRUE)

Arguments

dataname

(character(1)) name of the dataset

filtered

(logical) whether to return a filtered or unfiltered dataset


Method get_check()

Returns whether the datasets in the object has undergone a reproducibility check.

Usage

FilteredData$get_check()

Returns

logical


Method get_metadata()

Gets metadata for a given dataset.

Usage

FilteredData$get_metadata(dataname)

Arguments

dataname

(character(1)) name of the dataset

Returns

value of metadata for given data (or NULL if it does not exist)


Method get_join_keys()

Get join keys between two datasets.

Usage

FilteredData$get_join_keys()

Returns

(JoinKeys)


Method get_filter_overview()

Get filter overview table in form of X (filtered) / Y (non-filtered).

This is intended to be presented in the application. The content for each of the data names is defined in get_filter_overview_info method.

Usage

FilteredData$get_filter_overview(datanames)

Arguments

datanames

(character vector) names of the dataset

Returns

(matrix) matrix of observations and subjects of all datasets


Method get_keys()

Get keys for the dataset.

Usage

FilteredData$get_keys(dataname)

Arguments

dataname

(character(1)) name of the dataset

Returns

(character) keys of dataset


Method set_dataset()

Adds a dataset to this FilteredData.

Usage

FilteredData$set_dataset(data, dataname, metadata, label)

Arguments

data

(data.frame, MultiAssayExperiment)
data to be filtered.

dataname

(string)
the name of the dataset to be added to this object

metadata

(named list or NULL)
Field containing metadata about the dataset. Each element of the list should be atomic and length one.

label

(character(1))
Label to describe the dataset

Details

set_dataset creates a FilteredDataset object which keeps dataset for the filtering purpose. If this data has a parent specified in the JoinKeys object stored in private$join_keys then created FilteredDataset (child) gets linked with other FilteredDataset (parent). "Child" dataset return filtered data then dependent on the reactive filtered data of the "parent". See more in documentation of parent argument in FilteredDatasetDefault constructor.

Returns

(self) invisibly this FilteredData


Method set_join_keys()

Set the join_keys.

Usage

FilteredData$set_join_keys(join_keys)

Arguments

join_keys

(JoinKeys) join_key (converted to a nested list)

Returns

(self) invisibly this FilteredData


Method set_check()

Sets whether the datasets in the object have undergone a reproducibility check.

Usage

FilteredData$set_check(check)

Arguments

check

(logical) whether datasets have undergone reproducibility check

Returns

(self)


Method set_code()

Sets the R preprocessing code for single dataset.

Usage

FilteredData$set_code(code)

Arguments

code

(CodeClass)
preprocessing code that can be parsed to generate the unfiltered datasets

Returns

(self)


Method get_filter_state()

Gets states of all active FilterState objects.

Usage

FilteredData$get_filter_state()

Returns

A teal_slices object.


Method format()

Returns a formatted string representing this FilteredData object.

Usage

FilteredData$format(show_all = FALSE, trim_lines = TRUE)

Arguments

show_all

logical(1) passed to format.teal_slice

trim_lines

logical(1) passed to format.teal_slice

Returns

character(1) the formatted string


Method print()

Prints this FilteredData object.

Usage

FilteredData$print(...)

Arguments

...

additional arguments


Method set_filter_state()

Sets active filter states.

Usage

FilteredData$set_filter_state(state)

Arguments

state

either a named list list of filter selections or a teal_slices object
specification by list will be deprecated soon

Returns

NULL invisibly

Examples

utils::data(miniACC, package = "MultiAssayExperiment")

datasets <- teal.slice:::FilteredData$new(
  list(iris = list(dataset = iris),
       mae = list(dataset = miniACC)
  )
)
fs <-
  teal_slices(
    teal_slice(dataname = "iris", varname = "Sepal.Length", selected = c(5.1, 6.4),
               keep_na = TRUE, keep_inf = FALSE),
    teal_slice(dataname = "iris", varname = "Species", selected = c("setosa", "versicolor"),
               keep_na = FALSE),
    teal_slice(dataname = "mae", varname = "years_to_birth", selected = c(30, 50),
               keep_na = TRUE, keep_inf = FALSE),
    teal_slice(dataname = "mae", varname = "vital_status", selected = "1", keep_na = FALSE),
    teal_slice(dataname = "mae", varname = "gender", selected = "female", keep_na = TRUE),
    teal_slice(dataname = "mae", varname = "ARRAY_TYPE",
               selected = "", keep_na = TRUE, datalabel = "RPPAArray", arg = "subset")
  )
datasets$set_filter_state(state = fs)
shiny::isolate(datasets$get_filter_state())


Method remove_filter_state()

Removes one or more FilterState from a FilteredData object.

Usage

FilteredData$remove_filter_state(state)

Arguments

state

(teal_slices)
specifying FilterState objects to remove; teal_slices may contain only dataname and varname, other elements are ignored

Returns

NULL invisibly


Method clear_filter_states()

Remove all FilterStates of a FilteredDataset or all FilterStates of a FilteredData object.

Usage

FilteredData$clear_filter_states(datanames = self$datanames(), force = FALSE)

Arguments

datanames

(character)
datanames to remove their FilterStates or empty which removes all FilterStates in the FilteredData object

force

(logical(1))
include locked filter states

Returns

NULL invisibly

Module for the right filter panel in the teal app with a filter overview panel and a filter variable panel.

This panel contains info about the number of observations left in the (active) datasets and allows to filter the datasets.


Method ui_filter_panel()

Usage

FilteredData$ui_filter_panel(id)

Arguments

id

(character(1))
module id

Returns

shiny.tag Server function for filter panel


Method srv_filter_panel()

Usage

FilteredData$srv_filter_panel(id, active_datanames = self$datanames)

Arguments

id

(character(1))
an ID string that corresponds with the ID used to call the module's UI function.

active_datanames

function / reactive returning datanames that should be shown on the filter panel, must be a subset of the datanames argument provided to ui_filter_panel; if the function returns NULL (as opposed to character(0)), the filter panel will be hidden

Returns

moduleServer function which returns NULL


Method ui_active()

Server module responsible for displaying active filters.

Usage

FilteredData$ui_active(id)

Arguments

id

(character(1))
an ID string that corresponds with the ID used to call the module's UI function.

Returns

shiny.tag


Method srv_active()

Server module responsible for displaying active filters.

Usage

FilteredData$srv_active(id, active_datanames = self$datanames)

Arguments

id

(character(1))
an ID string that corresponds with the ID used to call the module's UI function.

active_datanames

(reactive)
defining subset of self$datanames() to be displayed.

Returns

moduleServer returning NULL


Method ui_add()

Server module responsible for displaying drop-downs with variables to add a filter.

Usage

FilteredData$ui_add(id)

Arguments

id

(character(1))
an ID string that corresponds with the ID used to call the module's UI function.

Returns

shiny.tag


Method srv_add()

Server module responsible for displaying drop-downs with variables to add a filter.

Usage

FilteredData$srv_add(id, active_datanames = reactive(self$datanames()))

Arguments

id

(character(1))
an ID string that corresponds with the ID used to call the module's UI function.

active_datanames

(reactive)
defining subset of self$datanames() to be displayed.

Returns

moduleServer returning NULL Creates the UI for the module showing counts for each dataset contrasting the filtered to the full unfiltered dataset

Per dataset, it displays the number of rows/observations in each dataset, the number of unique subjects.


Method ui_overview()

Usage

FilteredData$ui_overview(id)

Arguments

id

module id Server function to display the number of records in the filtered and unfiltered data


Method srv_overview()

Usage

FilteredData$srv_overview(id, active_datanames = self$datanames)

Arguments

id

(character(1))
an ID string that corresponds with the ID used to call the module's UI function.

active_datanames

(reactive)
returning datanames that should be shown on the filter panel, must be a subset of the datanames argument provided to ui_filter_panel; if the function returns NULL (as opposed to character(0)), the filter panel will be hidden.

Returns

moduleServer function which returns NULL


Method handle_active_datanames()

Method is deprecated. Provide resolved active_datanames to srv_filter_panel

Usage

FilteredData$handle_active_datanames(datanames)

Arguments

datanames

character vector datanames to pick

Returns

the intersection of self$datanames() and datanames


Method get_varlabels()

Method is deprecated. Please extract column labels directly from the data.

Usage

FilteredData$get_varlabels(dataname, variables = NULL)

Arguments

dataname

(character(1)) name of the dataset

variables

(character) variables to get labels for; if NULL, for all variables in data


Method get_varnames()

Method is deprecated, Please extract variable names directly from the data instead

Usage

FilteredData$get_varnames(dataname)

Arguments

dataname

(character) the name of the dataset


Method get_filterable_datanames()

Method is deprecated, please use self$datanames() instead

Usage

FilteredData$get_filterable_datanames()

Arguments

dataname

(character vector) names of the dataset


Method get_filterable_varnames()

Method is deprecated, please use self$get_filter_state() and retain attr(, "filterable_varnames") instead.

Usage

FilteredData$get_filterable_varnames(dataname)

Arguments

dataname

(character(1)) name of the dataset


Method set_filterable_varnames()

Method is deprecated, please use self$set_filter_state and teal_slices() with include_varnames instead.

Usage

FilteredData$set_filterable_varnames(dataname, varnames)

Arguments

dataname

(character(1)) name of the dataset

varnames

(character or NULL) variables which users can choose to filter the data; see self$get_filterable_varnames for more details


Method get_formatted_filter_state()

Method is deprecated, please use format.teal_slices on object returned from self$get_filter_state()

Usage

FilteredData$get_formatted_filter_state()


Method remove_all_filter_states()

Deprecated - please use clear_filter_states method.

Usage

FilteredData$remove_all_filter_states(datanames)

Arguments

datanames

(character)

Returns

NULL invisibly


Method clone()

The objects of this class are cloneable with this method.

Usage

FilteredData$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

library(shiny)
datasets <- teal.slice:::FilteredData$new(
  list(
    iris = list(dataset = iris),
    mtcars = list(dataset = mtcars)
  )
)

# get datanames
datasets$datanames()
#> [1] "iris"   "mtcars"

datasets$set_filter_state(
  teal_slices(teal_slice(dataname = "iris", varname = "Species", selected = "virginica"))
)
isolate(datasets$get_call("iris"))
#> $filter
#> iris <- dplyr::filter(iris, Species == "virginica")
#> 

datasets$set_filter_state(
  teal_slices(teal_slice(dataname = "mtcars", varname = "mpg", selected = c(15, 20)))
)

isolate(datasets$get_filter_state())
#> {
#>   "slices": [
#>     {
#>       "dataname"       : "iris",
#>       "varname"        : "Species",
#>       "id"             : "iris Species",
#>       "choices"        : ["setosa", "versicolor", "virgin...
#>       "selected"       : ["virginica"],
#>       "fixed"          : false,
#>       "anchored"       : false,
#>       "multiple"       : true
#>     },
#>     {
#>       "dataname"       : "mtcars",
#>       "varname"        : "mpg",
#>       "id"             : "mtcars mpg",
#>       "choices"        : [10.4, 34],
#>       "selected"       : [15, 20],
#>       "fixed"          : false,
#>       "anchored"       : false,
#>       "multiple"       : true
#>     }
#>   ],
#>   "attributes": {
#>     "include_varnames" : {
#>       "iris"           : ["Sepal.Length", "Sepal.Width", ...
#>       "mtcars"         : ["mpg", "cyl", "disp", "hp", "dr...
#>     },
#>     "count_type"       : "none",
#>     "allow_add"        : true
#>   }
#> } 
isolate(datasets$get_call("iris"))
#> $filter
#> iris <- dplyr::filter(iris, Species == "virginica")
#> 
isolate(datasets$get_call("mtcars"))
#> $filter
#> mtcars <- dplyr::filter(mtcars, mpg >= 15 & mpg <= 20)
#> 


## ------------------------------------------------
## Method `FilteredData$set_filter_state`
## ------------------------------------------------

utils::data(miniACC, package = "MultiAssayExperiment")

datasets <- teal.slice:::FilteredData$new(
  list(iris = list(dataset = iris),
       mae = list(dataset = miniACC)
  )
)
#> Loading required package: MultiAssayExperiment
#> Loading required package: SummarizedExperiment
#> Loading required package: MatrixGenerics
#> Loading required package: matrixStats
#> 
#> Attaching package: ‘MatrixGenerics’
#> The following objects are masked from ‘package:matrixStats’:
#> 
#>     colAlls, colAnyNAs, colAnys, colAvgsPerRowSet, colCollapse,
#>     colCounts, colCummaxs, colCummins, colCumprods, colCumsums,
#>     colDiffs, colIQRDiffs, colIQRs, colLogSumExps, colMadDiffs,
#>     colMads, colMaxs, colMeans2, colMedians, colMins, colOrderStats,
#>     colProds, colQuantiles, colRanges, colRanks, colSdDiffs, colSds,
#>     colSums2, colTabulates, colVarDiffs, colVars, colWeightedMads,
#>     colWeightedMeans, colWeightedMedians, colWeightedSds,
#>     colWeightedVars, rowAlls, rowAnyNAs, rowAnys, rowAvgsPerColSet,
#>     rowCollapse, rowCounts, rowCummaxs, rowCummins, rowCumprods,
#>     rowCumsums, rowDiffs, rowIQRDiffs, rowIQRs, rowLogSumExps,
#>     rowMadDiffs, rowMads, rowMaxs, rowMeans2, rowMedians, rowMins,
#>     rowOrderStats, rowProds, rowQuantiles, rowRanges, rowRanks,
#>     rowSdDiffs, rowSds, rowSums2, rowTabulates, rowVarDiffs, rowVars,
#>     rowWeightedMads, rowWeightedMeans, rowWeightedMedians,
#>     rowWeightedSds, rowWeightedVars
#> Loading required package: GenomicRanges
#> Loading required package: stats4
#> Loading required package: BiocGenerics
#> 
#> Attaching package: ‘BiocGenerics’
#> The following objects are masked from ‘package:stats’:
#> 
#>     IQR, mad, sd, var, xtabs
#> The following objects are masked from ‘package:base’:
#> 
#>     Filter, Find, Map, Position, Reduce, anyDuplicated, aperm, append,
#>     as.data.frame, basename, cbind, colnames, dirname, do.call,
#>     duplicated, eval, evalq, get, grep, grepl, intersect, is.unsorted,
#>     lapply, mapply, match, mget, order, paste, pmax, pmax.int, pmin,
#>     pmin.int, rank, rbind, rownames, sapply, setdiff, sort, table,
#>     tapply, union, unique, unsplit, which.max, which.min
#> Loading required package: S4Vectors
#> 
#> Attaching package: ‘S4Vectors’
#> The following object is masked from ‘package:utils’:
#> 
#>     findMatches
#> The following objects are masked from ‘package:base’:
#> 
#>     I, expand.grid, unname
#> Loading required package: IRanges
#> Loading required package: GenomeInfoDb
#> Loading required package: Biobase
#> Welcome to Bioconductor
#> 
#>     Vignettes contain introductory material; view with
#>     'browseVignettes()'. To cite Bioconductor, see
#>     'citation("Biobase")', and for packages 'citation("pkgname")'.
#> 
#> Attaching package: ‘Biobase’
#> The following object is masked from ‘package:MatrixGenerics’:
#> 
#>     rowMedians
#> The following objects are masked from ‘package:matrixStats’:
#> 
#>     anyMissing, rowMedians
fs <-
  teal_slices(
    teal_slice(dataname = "iris", varname = "Sepal.Length", selected = c(5.1, 6.4),
               keep_na = TRUE, keep_inf = FALSE),
    teal_slice(dataname = "iris", varname = "Species", selected = c("setosa", "versicolor"),
               keep_na = FALSE),
    teal_slice(dataname = "mae", varname = "years_to_birth", selected = c(30, 50),
               keep_na = TRUE, keep_inf = FALSE),
    teal_slice(dataname = "mae", varname = "vital_status", selected = "1", keep_na = FALSE),
    teal_slice(dataname = "mae", varname = "gender", selected = "female", keep_na = TRUE),
    teal_slice(dataname = "mae", varname = "ARRAY_TYPE",
               selected = "", keep_na = TRUE, datalabel = "RPPAArray", arg = "subset")
  )
datasets$set_filter_state(state = fs)
#> [WARN] 2023-08-14 13:45:19.3671 pid:646 token:[] teal.slice filters for columns: ARRAY_TYPE excluded from mae
shiny::isolate(datasets$get_filter_state())
#> {
#>   "slices": [
#>     {
#>       "dataname"       : "iris",
#>       "varname"        : "Sepal.Length",
#>       "id"             : "iris Sepal.Length",
#>       "choices"        : [4.2999999999999998, 7.900000000...
#>       "selected"       : [5.0999999999999996, 6.400000000...
#>       "keep_na"        : true,
#>       "keep_inf"       : false,
#>       "fixed"          : false,
#>       "anchored"       : false,
#>       "multiple"       : true
#>     },
#>     {
#>       "dataname"       : "iris",
#>       "varname"        : "Species",
#>       "id"             : "iris Species",
#>       "choices"        : ["setosa", "versicolor", "virgin...
#>       "selected"       : ["setosa", "versicolor"],
#>       "keep_na"        : false,
#>       "fixed"          : false,
#>       "anchored"       : false,
#>       "multiple"       : true
#>     },
#>     {
#>       "dataname"       : "mae",
#>       "varname"        : "years_to_birth",
#>       "id"             : "mae years_to_birth",
#>       "choices"        : [14, 83],
#>       "selected"       : [30, 50],
#>       "keep_na"        : true,
#>       "keep_inf"       : false,
#>       "fixed"          : false,
#>       "anchored"       : false,
#>       "multiple"       : true
#>     },
#>     {
#>       "dataname"       : "mae",
#>       "varname"        : "vital_status",
#>       "id"             : "mae vital_status",
#>       "choices"        : ["0", "1"],
#>       "selected"       : ["1"],
#>       "keep_na"        : false,
#>       "fixed"          : false,
#>       "anchored"       : false,
#>       "multiple"       : true
#>     },
#>     {
#>       "dataname"       : "mae",
#>       "varname"        : "gender",
#>       "id"             : "mae gender",
#>       "choices"        : ["female", "male"],
#>       "selected"       : ["female"],
#>       "keep_na"        : true,
#>       "fixed"          : false,
#>       "anchored"       : false,
#>       "multiple"       : true
#>     }
#>   ],
#>   "attributes": {
#>     "include_varnames" : {
#>       "iris"           : ["Sepal.Length", "Sepal.Width", ...
#>       "mae"            : ["patientID", "years_to_birth", ...
#>     },
#>     "count_type"       : "none",
#>     "allow_add"        : true
#>   }
#> }