Skip to content

Commit

Permalink
makes the dataset filter an optional feature
Browse files Browse the repository at this point in the history
  • Loading branch information
zsigmas committed Jan 15, 2025
1 parent ec9499f commit d5dac98
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 36 deletions.
96 changes: 61 additions & 35 deletions R/app_server.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ app_server <- function(input = NULL, output = NULL, session = NULL) {
"filter_data" = get_config("filter_data"),
"filter_key" = get_config("filter_key"),
"startup_msg" = get_config("startup_msg"),
"reload_period" = get_config("reload_period")
"reload_period" = get_config("reload_period"),
"use_dataset_filter" = get_config("use_dataset_filter")
)

app_server_(input, output, session, opts)
Expand Down Expand Up @@ -93,6 +94,7 @@ app_server_ <- function(input, output, session, opts) {
filter_key <- opts[["filter_key"]]
startup_msg <- opts[["startup_msg"]]
reload_period <- opts[["reload_period"]]
use_dataset_filter <- opts[["use_dataset_filter"]]

datasets_filters_info <- get_dataset_filters_info(data, filter_data)

Expand Down Expand Up @@ -132,7 +134,12 @@ app_server_ <- function(input, output, session, opts) {
shiny::reactive(unfiltered_dataset()[[filter_data]])
)

dataset_filters <- local({

if(use_dataset_filter) {

log_inform("Dataset filter server")

dataset_filters <- local({
l <- vector(mode = "list", length = length(datasets_filters_info))
names(l) <- names(datasets_filters_info)
for (idx in seq_along(datasets_filters_info)) {
Expand All @@ -150,7 +157,7 @@ app_server_ <- function(input, output, session, opts) {
l
})

filtered_dataset <- shinymeta::metaReactive({
filtered_dataset <- shinymeta::metaReactive({
# dv.filter returns a logical vector. This contemplates the case of empty lists
shiny::req(is.logical(global_filtered_values()))

Expand Down Expand Up @@ -189,6 +196,57 @@ app_server_ <- function(input, output, session, opts) {
)
})

tab_ids <- c("__tabset_0__", names(opts[["module_info"]][["tab_group_names"]]))
shiny::observeEvent(
{
purrr::map(tab_ids, ~ input[[.x]])
},
{
current_tab <- "__tabset_0__"
zero_tabs <- length(input[["__tabset_0__"]]) == 0
if (!zero_tabs) {
while (!current_tab %in% opts[["module_info"]][["module_id_list"]]) {
current_tab <- input[[current_tab]]
}
}

used_ds <- used_datasets[[current_tab]]
all_nm <- names(datasets_filters_info)
if (!zero_tabs && !is.null(used_ds)) {
used_nm <- intersect(used_datasets[[current_tab]], names(datasets_filters_info))
unused_nm <- setdiff(all_nm, used_nm)
} else {
used_nm <- all_nm
unused_nm <- character(0)
}

for (nm in unused_nm) {
shinyjs::hide(datasets_filters_info[[nm]][["id_cont"]])
}

for (nm in used_nm) {
shinyjs::show(datasets_filters_info[[nm]][["id_cont"]])
}
}
)

} else {

log_inform("Single filter server")

filtered_dataset <- shinymeta::metaReactive({
# dv.filter returns a logical vector. This contemplates the case of empty lists
shiny::req(is.logical(global_filtered_values()))
log_inform("New filter applied")
filtered_key_values <- unfiltered_dataset()[[filter_data]][[filter_key]][global_filtered_values()] # nolint
purrr::map(
unfiltered_dataset(),
~ dplyr::filter(.x, .data[[filter_key]] %in% filtered_key_values) # nolint
)
})

}

# Prepare module_output argument
module_output_env <- rlang::current_env()
module_output_func <- function() {
Expand Down Expand Up @@ -270,39 +328,7 @@ app_server_ <- function(input, output, session, opts) {
}


tab_ids <- c("__tabset_0__", names(opts[["module_info"]][["tab_group_names"]]))
shiny::observeEvent(
{
purrr::map(tab_ids, ~ input[[.x]])
},
{
current_tab <- "__tabset_0__"
zero_tabs <- length(input[["__tabset_0__"]]) == 0
if (!zero_tabs) {
while (!current_tab %in% opts[["module_info"]][["module_id_list"]]) {
current_tab <- input[[current_tab]]
}
}

used_ds <- used_datasets[[current_tab]]
all_nm <- names(datasets_filters_info)
if (!zero_tabs && !is.null(used_ds)) {
used_nm <- intersect(used_datasets[[current_tab]], names(datasets_filters_info))
unused_nm <- setdiff(all_nm, used_nm)
} else {
used_nm <- all_nm
unused_nm <- character(0)
}

for (nm in unused_nm) {
shinyjs::hide(datasets_filters_info[[nm]][["id_cont"]])
}

for (nm in used_nm) {
shinyjs::show(datasets_filters_info[[nm]][["id_cont"]])
}
}
)

#### Report modal

Expand Down
5 changes: 4 additions & 1 deletion R/app_ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ app_ui <- function(request_id) {
data <- get_config("data")
module_info <- get_config("module_info")
filter_data <- get_config("filter_data")
use_dataset_filter <- get_config("use_dataset_filter")

log_inform("Initializing HTML template UI")
log_inform(glue::glue("Available modules (N): {length(module_info[[\"ui_list\"]])}"))
Expand Down Expand Up @@ -71,7 +72,8 @@ app_ui <- function(request_id) {
dv.filter::data_filter_ui(ns("global_filter"))
)
),
shiny::div(
if (use_dataset_filter) {
shiny::div(
class = "c-well shiny_filter",
shiny::tags$label(
"Dataset Filter(s)",
Expand All @@ -80,6 +82,7 @@ app_ui <- function(request_id) {
),
dataset_filters_ui
)
}
)
)

Expand Down
2 changes: 2 additions & 0 deletions R/run_app.R
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ run_app <- function(data = NULL,
azure_options = NULL,
reload_period = NULL,
enableBookmarking = "server", # nolint
use_dataset_filter = FALSE,
.launch = TRUE) {
check_deprecated_calls(filter_data)

Expand All @@ -68,6 +69,7 @@ run_app <- function(data = NULL,
config[["startup_msg"]] <- check_startup_msg(startup_msg)
config[["title"]] <- title
config[["reload_period"]] <- get_reload_period(check_reload_period(reload_period))
config[["use_dataset_filter"]] <- use_dataset_filter

check_meta_mtime_attribute(data)

Expand Down

0 comments on commit d5dac98

Please sign in to comment.