From bc5a6630af595f42ef6b8b9460f41426aac51075 Mon Sep 17 00:00:00 2001 From: Chris Beeley Date: Thu, 9 Dec 2021 14:55:45 +0000 Subject: [PATCH 1/3] Initial changes to tidy_all_trusts to handle empty datasets --- R/app_server.R | 2 +- R/tidy_data.R | 24 ++++++++++++++++-------- app.R | 2 +- man/tidy_all_trusts.Rd | 2 +- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/R/app_server.R b/R/app_server.R index afd75f0..1e114af 100644 --- a/R/app_server.R +++ b/R/app_server.R @@ -20,7 +20,7 @@ app_server <- function( input, output, session ) { db_data <- dplyr::tbl(pool, dbplyr::in_schema("TEXT_MINING", get_golem_config("trust_name"))) %>% - tidy_all_trusts(conn = pool, trust_id = get_golem_config("trust_name")) + tidy_all_trusts(conn = pool) # vector of sentiment names diff --git a/R/tidy_data.R b/R/tidy_data.R index 614edb7..dc248cc 100644 --- a/R/tidy_data.R +++ b/R/tidy_data.R @@ -12,13 +12,21 @@ #' Chris Beeley #' @section Last updated date: #' 2021-04-25 -tidy_all_trusts <- function(data, conn, trust_id) { +tidy_all_trusts <- function(data, conn) { - data %>% - dplyr::mutate(category = dplyr::case_when( - is.null(comment_txt) ~ NA_character_, - is.na(comment_txt) ~ NA_character_, - comment_txt %in% c("NULL", "NA", "N/A") ~ NA_character_, - TRUE ~ category - )) + # this line only works if there is data in the table + + if(data %>% + dplyr::tally() %>% + dplyr::pull(n) > 0) { + + data %>% + dplyr::mutate(category = dplyr::case_when( + is.null(comment_txt) ~ NA_character_, + is.na(comment_txt) ~ NA_character_, + comment_txt %in% c("NULL", "NA", "N/A") ~ NA_character_, + TRUE ~ category + )) + + } } diff --git a/app.R b/app.R index abf966f..e40688e 100644 --- a/app.R +++ b/app.R @@ -2,7 +2,7 @@ # To deploy, run: rsconnect::deployApp() # Or use the blue button on top of this file -Sys.setenv("R_CONFIG_ACTIVE" = "trust_a") +Sys.setenv("R_CONFIG_ACTIVE" = "trust_b") pkgload::load_all(export_all = FALSE, helpers = FALSE, diff --git a/man/tidy_all_trusts.Rd b/man/tidy_all_trusts.Rd index db745be..e95c68f 100644 --- a/man/tidy_all_trusts.Rd +++ b/man/tidy_all_trusts.Rd @@ -4,7 +4,7 @@ \alias{tidy_all_trusts} \title{Tidy patient experience data} \usage{ -tidy_all_trusts(data, conn, trust_id) +tidy_all_trusts(data, conn) } \arguments{ \item{data}{dataframe or SQL object, that you can make with get_px_exp()} From 0ac648f8c1c07c6a68ba8f5fcc541463148fd315 Mon Sep 17 00:00:00 2001 From: Chris Beeley Date: Fri, 10 Dec 2021 12:05:17 +0000 Subject: [PATCH 2/3] Add req() to the renderUI() functions --- R/app_server.R | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/R/app_server.R b/R/app_server.R index 1e114af..cf80d1c 100644 --- a/R/app_server.R +++ b/R/app_server.R @@ -30,6 +30,12 @@ app_server <- function( input, output, session ) { dplyr::pull() %>% sort() + # find out if there is data in the table + + data_exists <- db_data %>% + dplyr::tally() %>% + dplyr::pull(n) > 0 + # store values of demographics and location_1 from last 3 years interpolate_date <- Sys.Date() @@ -47,11 +53,8 @@ app_server <- function( input, output, session ) { output$filter_location_1 <- renderUI({ - if(!isTruthy(get_golem_config("location_1"))){ + req(get_golem_config("location_1")) - return() - } - location_1_choices <- date_filter() %>% dplyr::distinct(location_1) %>% dplyr::mutate(location_1 = dplyr::na_if(location_1, "Unknown")) %>% @@ -69,12 +72,8 @@ app_server <- function( input, output, session ) { output$filter_location_2 <- renderUI({ - if(!isTruthy(get_golem_config("location_2"))){ - - return() - } - - + req(get_golem_config("location_2")) + location_2_choices <- date_filter() if(isTruthy(input$select_location_1)){ # filter by location_1 if exists @@ -99,11 +98,8 @@ app_server <- function( input, output, session ) { output$filter_location_3 <- renderUI({ - if(!isTruthy(get_golem_config("location_3"))){ + req(get_golem_config("location_3")) - return() - } - location_3_choices <- date_filter() if(isTruthy(input$select_location_1)){ # filter by location_1 if exists @@ -150,6 +146,7 @@ app_server <- function( input, output, session ) { dplyr::filter(date > !!input$date_range[1], date < !!input$date_range[2]) }) + filter_data <- reactive({ if(get_golem_config("trust_name") == "demo_trust"){ From 66f917fffea6e6f47dfcc4432c3d4c9ccdb5a217 Mon Sep 17 00:00:00 2001 From: Chris Beeley Date: Fri, 10 Dec 2021 12:29:38 +0000 Subject: [PATCH 3/3] Check that data exists in filtering functions --- R/app_server.R | 3 +++ R/tidy_data.R | 2 ++ 2 files changed, 5 insertions(+) diff --git a/R/app_server.R b/R/app_server.R index cf80d1c..0100fcd 100644 --- a/R/app_server.R +++ b/R/app_server.R @@ -54,6 +54,7 @@ app_server <- function( input, output, session ) { output$filter_location_1 <- renderUI({ req(get_golem_config("location_1")) + req(data_exists) location_1_choices <- date_filter() %>% dplyr::distinct(location_1) %>% @@ -73,6 +74,7 @@ app_server <- function( input, output, session ) { output$filter_location_2 <- renderUI({ req(get_golem_config("location_2")) + req(data_exists) location_2_choices <- date_filter() @@ -99,6 +101,7 @@ app_server <- function( input, output, session ) { output$filter_location_3 <- renderUI({ req(get_golem_config("location_3")) + req(data_exists) location_3_choices <- date_filter() diff --git a/R/tidy_data.R b/R/tidy_data.R index dc248cc..4248569 100644 --- a/R/tidy_data.R +++ b/R/tidy_data.R @@ -27,6 +27,8 @@ tidy_all_trusts <- function(data, conn) { comment_txt %in% c("NULL", "NA", "N/A") ~ NA_character_, TRUE ~ category )) + } else { + data } }