From 348288127c20e6bffc8b96e0a682974901a2b413 Mon Sep 17 00:00:00 2001 From: Trevor Riley Date: Fri, 8 Nov 2024 13:15:59 -0500 Subject: [PATCH] Refactor filter update to handle empty and NA values in citation data - Added checks for `cite_source`, `cite_label`, and `cite_string` to handle cases where values are empty or NA, setting them to NULL if no valid entries exist. - Streamlined the filtering process by removing "unknown" entries and splitting values only after ensuring non-empty data. - This change improves robustness in filtering by preventing empty or "unknown" entries from appearing in the final list of sources, labels, and strings. --- inst/shiny-app/CiteSource/app.R | 44 ++++++++++++++++++++++++++------- 1 file changed, 35 insertions(+), 9 deletions(-) diff --git a/inst/shiny-app/CiteSource/app.R b/inst/shiny-app/CiteSource/app.R index 60bc112..b01c2f7 100644 --- a/inst/shiny-app/CiteSource/app.R +++ b/inst/shiny-app/CiteSource/app.R @@ -466,18 +466,44 @@ server <- function(input, output, session) { shiny::observe({ if (nrow(rv$latest_unique) > 0) { - sources <- unique(rv$latest_unique$cite_source) %>% stringr::str_split(", ") %>% unlist() %>% unique() %>% sort() - labels <- unique(rv$latest_unique$cite_label) %>% stringr::str_split(", ") %>% unlist() %>% unique() %>% sort() - strings <- unique(rv$latest_unique$cite_string) %>% stringr::str_split(", ") %>% unlist() %>% unique() %>% sort() + # Handle cite_source + sources <- rv$latest_unique$cite_source + if (all(is.na(sources) | sources == "")) { + sources <- NULL # Leave it as NULL or NA + } else { + sources <- unique(sources[!is.na(sources) & sources != ""]) %>% + stringr::str_split(", ") %>% + unlist() %>% + unique() %>% + sort() + } - sources <- sources[sources != "unknown"] - labels <- labels[labels != "unknown"] - strings <- strings[strings != "unknown"] + # Handle cite_label + labels <- rv$latest_unique$cite_label + if (all(is.na(labels) | labels == "")) { + labels <- NULL # Leave it as NULL or NA + } else { + labels <- unique(labels[!is.na(labels) & labels != ""]) %>% + stringr::str_split(", ") %>% + unlist() %>% + unique() %>% + sort() + + } - if (any(rv$latest_unique$cite_source == "unknown")) sources <- c(sources, "_blank_") - if (any(rv$latest_unique$cite_label == "unknown")) labels <- c(labels, "_blank_") - if (any(rv$latest_unique$cite_string == "unknown")) strings <- c(strings, "_blank_") + # Handle cite_string + strings <- rv$latest_unique$cite_string + if (all(is.na(strings) | strings == "")) { + strings <- NULL # Leave it as NULL or NA + } else { + strings <- unique(strings[!is.na(strings) & strings != ""]) %>% + stringr::str_split(", ") %>% + unlist() %>% + unique() %>% + sort() + } + # Update select inputs shiny::updateSelectInput(inputId = "sources_visual", choices = sources, selected = sources) shiny::updateSelectInput(inputId = "labels_visual", choices = labels, selected = labels) shiny::updateSelectInput(inputId = "strings_visual", choices = strings, selected = strings)