Skip to content

Commit

Permalink
Refactor filter update to handle empty and NA values in citation data
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
TNRiley committed Nov 8, 2024
1 parent e5f08da commit 3482881
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions inst/shiny-app/CiteSource/app.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3482881

Please sign in to comment.