FilteredData | R Documentation |
Manages filtering of all datasets in the application or module.
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:
filtered
: whether to return a filtered result or not
dataname
: the name of one of the datasets in this FilteredData
object
varname
: one of the columns in a dataset
new()
Initialize a FilteredData
object.
FilteredData$new(data_objects, join_keys = teal.data::join_keys())
data_objects
(named list
)
List of data objects.
Names of the list will be used as dataname
for respective datasets.
join_keys
(join_keys
) optional joining keys, see teal.data::join_keys()
.
datanames()
Gets datanames
.
FilteredData$datanames()
The datanames
are returned in the order in which they must be evaluated (in case of dependencies).
Character vector.
get_datalabel()
Gets data label for the dataset.
Useful to display in Show R Code
.
FilteredData$get_datalabel(dataname)
dataname
(character(1)
) name of the dataset
Character string.
set_available_teal_slices()
Set list of external filter states available for activation.
FilteredData$set_available_teal_slices(x)
x
(reactive
)
should return teal_slices
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
object,
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
.
NULL
, invisibly.
get_available_teal_slices()
Get list of filter states available for this object.
FilteredData$get_available_teal_slices()
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.
reactive
that returns teal_slices
.
get_call()
Gets a call
to filter the dataset according to the filter state.
FilteredData$get_call(dataname)
dataname
(character(1)
) name of the dataset
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.
A list of call
s.
get_data()
Gets filtered or unfiltered dataset.
For filtered = FALSE
, the original data set with set_data
is returned including all attributes.
FilteredData$get_data(dataname, filtered = TRUE)
dataname
(character(1)
) name of the dataset.
filtered
(logical(1)
) whether to return a filtered or unfiltered dataset.
A data object, a data.frame
or a MultiAssayExperiment
.
get_join_keys()
Get join keys between two datasets.
FilteredData$get_join_keys()
join_keys
get_filter_overview()
Creates filter overview table to be displayed in the application.
One row is created per dataset, according to the get_filter_overview
methods
of the contained FilteredDataset
objects.
FilteredData$get_filter_overview(datanames)
datanames
(character
) vector of dataset names.
A data.frame
listing the numbers of observations in all datasets.
get_keys()
Get keys for the dataset.
FilteredData$get_keys(dataname)
dataname
(character(1)
) name of the dataset.
Character vector of key column names.
set_dataset()
Adds a dataset to this FilteredData
.
FilteredData$set_dataset(data, dataname)
data
(data.frame
or MultiAssayExperiment
)
data to be filtered.
dataname
(character(1)
)
the name of the dataset
to be added to this object.
set_dataset
creates a FilteredDataset
object which keeps dataset
for the filtering purpose.
If this data has a parent specified in the join_keys
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 DataframeFilteredDataset
constructor.
self
, invisibly.
set_join_keys()
Set the join_keys
.
FilteredData$set_join_keys(join_keys)
join_keys
(join_keys
), see teal.data::join_keys()
.
self
, invisibly.
get_filter_state()
Gets states of all contained FilterState
objects.
FilteredData$get_filter_state()
A teal_slices
object.
format()
Returns a formatted string representing this FilteredData
object.
FilteredData$format(show_all = FALSE, trim_lines = TRUE)
show_all
(logical(1)
) passed to format.teal_slice
.
trim_lines
(logical(1)
) passed to format.teal_slice
.
character(1)
the formatted string.
print()
Prints this FilteredData
object.
FilteredData$print(...)
...
additional arguments passed to format
.
set_filter_state()
Sets active filter states.
FilteredData$set_filter_state(state)
state
(teal_slices
)
NULL
, invisibly.
remove_filter_state()
Removes one or more FilterState
from a FilteredData
object.
FilteredData$remove_filter_state(state)
state
(teal_slices
)
specifying FilterState
objects to remove;
teal_slice
s may contain only dataname
and varname
, other elements are ignored.
NULL
, invisibly.
clear_filter_states()
Remove all FilterStates
of a FilteredDataset
or all FilterStates
of a FilteredData
object.
FilteredData$clear_filter_states(datanames = self$datanames(), force = FALSE)
datanames
(character
)
names of datasets for which to remove all filter states.
Defaults to all datasets in this FilteredData
object.
force
(logical(1)
)
flag specifying whether to include anchored filter states.
NULL
, invisibly.
ui_filter_panel()
top-level shiny
module for the filter panel in the teal
app.
Contains 1) filter overview panel, 2) filter active panel, and 3) add filters panel.
FilteredData$ui_filter_panel(id)
id
(character(1)
)
shiny
module instance id.
shiny.tag
srv_filter_panel()
Server function for filter panel.
FilteredData$srv_filter_panel(id, active_datanames = self$datanames)
id
(character(1)
)
shiny
module instance id.
active_datanames
(function
or reactive
)
returning datanames
that should be shown on the filter panel.
Must be a subset of the datanames
in this FilteredData
.
If the function returns NULL
(as opposed to character(0)
),
the filter panel will be hidden.
NULL
.
ui_active()
Server module responsible for displaying active filters.
FilteredData$ui_active(id)
id
(character(1)
)
shiny
module instance id.
shiny.tag
srv_active()
Server module responsible for displaying active filters.
FilteredData$srv_active(id, active_datanames = self$datanames)
id
(character(1)
)
shiny
module instance id.
active_datanames
(reactive
)
defining subset of self$datanames()
to be displayed.
NULL
.
ui_add()
Server module responsible for displaying drop-downs with variables to add a filter.
FilteredData$ui_add(id)
id
(character(1)
)
shiny
module instance id.
shiny.tag
srv_add()
Server module responsible for displaying drop-downs with variables to add a filter.
FilteredData$srv_add(id, active_datanames = reactive(self$datanames()))
id
(character(1)
)
shiny
module instance id.
active_datanames
(reactive
)
defining subset of self$datanames()
to be displayed.
NULL
.
ui_overview()
Creates the UI definition 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.
FilteredData$ui_overview(id)
id
(character(1)
)
shiny
module instance id.
srv_overview()
Server function to display the number of records in the filtered and unfiltered data.
FilteredData$srv_overview(id, active_datanames = self$datanames)
id
(character(1)
)
shiny
module instance id.
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.
NULL
.
clone()
The objects of this class are cloneable with this method.
FilteredData$clone(deep = FALSE)
deep
Whether to make a deep clone.
# use non-exported function from teal.slice
FilteredData <- getFromNamespace("FilteredData", "teal.slice")
library(shiny)
datasets <- FilteredData$new(list(iris = iris, mtcars = mtcars))
# get datanames
datasets$datanames()
datasets$set_filter_state(
teal_slices(teal_slice(dataname = "iris", varname = "Species", selected = "virginica"))
)
isolate(datasets$get_call("iris"))
datasets$set_filter_state(
teal_slices(teal_slice(dataname = "mtcars", varname = "mpg", selected = c(15, 20)))
)
isolate(datasets$get_filter_state())
isolate(datasets$get_call("iris"))
isolate(datasets$get_call("mtcars"))
### set_filter_state
library(shiny)
data(miniACC, package = "MultiAssayExperiment")
datasets <- FilteredData$new(list(iris = iris, mae = 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, experiment = "RPPAArray", arg = "subset"
)
)
datasets$set_filter_state(state = fs)
isolate(datasets$get_filter_state())
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.