From d955a89d2d80d4bff2628c121bebf9e86440e9f6 Mon Sep 17 00:00:00 2001 From: apigap01 Date: Fri, 22 Nov 2024 12:02:56 +0000 Subject: [PATCH 1/4] Update with 22/23 data and convert chart from plotly to highcharter --- data_preparation.R | 12 +++++++----- shiny_app/global.R | 10 +++------- shiny_app/server.R | 31 ++++++++++++------------------- shiny_app/ui.R | 11 ++++++----- 4 files changed, 28 insertions(+), 36 deletions(-) diff --git a/data_preparation.R b/data_preparation.R index 64583c8..d7a99f8 100644 --- a/data_preparation.R +++ b/data_preparation.R @@ -2,13 +2,15 @@ library(tidyr) library(dplyr) -data_allergy <- readRDS("/PHI_conf/ScotPHO/Website/Topics/Allergy/sept2022_update/allergy_scotland_chart.rds") %>% +filepath <- "/PHI_conf/ScotPHO/Website/Charts/Health Conditions/Allergic Conditions/" + +data_allergy <- readRDS(paste0(filepath, "allergy_scotland_chart.rds")) |> mutate(rate = round(rate, 1), # round numbers more (one decimal place) numerator = case_when(numerator < 5 ~ NA_real_, - TRUE ~ numerator)) %>% # supression of under 5 - gather(measure, value, -c(type, year)) %>% + TRUE ~ numerator)) |> # supression of under 5 + gather(measure, value, -c(type, year)) |> mutate(measure = recode(measure, "numerator" = "Number", "rate" = "Rate")) -saveRDS(data_allergy, "shiny_app/allergy_scotland_chart.rds") +saveRDS(data_allergy, paste0(filepath, "allergy_scotland_chart.rds")) -## END \ No newline at end of file +## END diff --git a/shiny_app/global.R b/shiny_app/global.R index 290d9be..699a7af 100644 --- a/shiny_app/global.R +++ b/shiny_app/global.R @@ -6,18 +6,14 @@ library(dplyr) #data manipulation library(plotly) #charts +library(highcharter) #charts library(shiny) library(shinymanager) +library(phsstyles) #chart colours -data_allergy <- readRDS("allergy_scotland_chart.rds") +data_allergy <- readRDS(paste0(filepath, "allergy_scotland_chart.rds")) #Use for selection of conditions condition_list <- sort(unique(data_allergy$type)) -#ScotPHO logo. -#Needs to be https address or if local in code 64 (the latter does not work with 4.7 plotly) -scotpho_logo <- list(source ="https://raw.githubusercontent.com/ScotPHO/plotly-charts/master/scotpho.png", - xref = "paper", yref = "paper", - x= -0.09, y= 1.2, sizex = 0.22, sizey = 0.18, opacity = 1) - ## END \ No newline at end of file diff --git a/shiny_app/server.R b/shiny_app/server.R index f206d0b..eb987de 100644 --- a/shiny_app/server.R +++ b/shiny_app/server.R @@ -1,7 +1,7 @@ ############################. ## Server ---- ############################. -credentials_allergy <- readRDS("credentials.rds") +#credentials_allergy <- readRDS("credentials.rds") function(input, output) { @@ -21,29 +21,22 @@ function(input, output) { ############################. #Visualization - output$chart <- renderPlotly({ + output$line_chart <- renderHighchart({ #Data for condition - data_condition <- data_allergy %>% subset(type %in% input$conditions & measure==input$measure) + data_condition <- data_allergy |> subset(type %in% input$conditions & measure == input$measure) #y axis title yaxistitle <- case_when(input$measure == "Number" ~ "Number of hospital admissions", input$measure == "Rate" ~ "Hospital admissions
per 100,000 population") - plot <- plot_ly(data=data_condition, x=~year, y = ~value, color = ~type, - colors = c('#abd9e9', '#74add1', '#4575b4', '#313695', '#022031'), - type = "scatter", mode = 'lines', - width = 650, height = 350) %>% - #Layout - layout(annotations = list(), #It needs this because of a buggy behaviour - yaxis = list(title = yaxistitle, rangemode="tozero", fixedrange=TRUE), - xaxis = list(title = "Financial year", fixedrange=TRUE, tickangle = 270), - font = list(family = 'Arial, sans-serif'), #font - margin = list(pad = 4, t = 50, r = 30), #margin-paddings - hovermode = 'false', # to get hover compare mode as default - images = scotpho_logo) %>% - config(displayModeBar= T, displaylogo = F) # taking out plotly logo and collaborate button - } - ) + data_condition |> + hchart("line", hcaes(y = value, x = year, group = type)) |> + hc_colors(c('#abd9e9', '#74add1', '#4575b4', '#313695', '#022031')) |> + hc_xAxis(title = list(text = "Year")) |> + hc_yAxis(title = list(text = yaxistitle), min = 0) |> + hc_legend(align = "left", verticalAlign = "top") + + }) -} # end of server part \ No newline at end of file +} # end of server part diff --git a/shiny_app/ui.R b/shiny_app/ui.R index 9cda47e..a0df58f 100644 --- a/shiny_app/ui.R +++ b/shiny_app/ui.R @@ -3,7 +3,7 @@ ############################. #Height and widths as percentages to allow responsiveness #Using divs as issues with classing css -secure_app( +#secure_app( fluidPage(style="width: 650px; height: 500px; ", div(style= "width:100%", #Filters on top of page h4("Chart 1. Hospital admissions for different allergic conditions"), @@ -12,14 +12,15 @@ fluidPage(style="width: 650px; height: 500px; ", choices = c("Number", "Rate"), selected = "Rate") ), div(style = "width: 50%; float: left;", - selectizeInput("conditions", label = "Select one or more allergic conditions (up to four)", + selectizeInput("conditions", label = "Select up to four allergic conditions", choices = condition_list, multiple = TRUE, selected = "All allergies", options = list(maxItems =4L))) ), div(style= "width:100%; float: left;", #Main panel - plotlyOutput("chart", width = "100%", height = "350px"), + #plotlyOutput("chart", width = "100%", height = "350px"), + highchartOutput("line_chart"), p(div(style = "width: 25%; float: left;", #Footer - HTML("Source: PHS, SMR 01")), + HTML("Source: PHS, SMR 01")), div(style = "width: 25%; float: left;", downloadLink('download_data', 'Download data')), div(style = "width: 100%; float: left;", @@ -30,4 +31,4 @@ fluidPage(style="width: 650px; height: 500px; ", ) ) )#fluid page bracket -) \ No newline at end of file +#) \ No newline at end of file From 580035e84611af9657385c0015a4e9c6084db1b7 Mon Sep 17 00:00:00 2001 From: apigap01 Date: Thu, 28 Nov 2024 16:48:51 +0000 Subject: [PATCH 2/4] Final tweaks for deployment --- .gitignore | 4 ++++ shiny_app/global.R | 2 +- shiny_app/server.R | 2 +- shiny_app/ui.R | 4 ++-- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 8e31cd5..2734b21 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,7 @@ allergies_chart.Rproj deploying_app.R testing.R create_credentials.R +credentials.R # packrat library folder (automatically added due to packract being initialised) packrat/lib*/ @@ -21,6 +22,9 @@ www/ # folder created when deploying an app # rsconnect/ +# admin folder # +admin/ + # Common text files that may contain data # *.[cC][sS][vV] *.[tT][xX][tT] diff --git a/shiny_app/global.R b/shiny_app/global.R index 699a7af..c945445 100644 --- a/shiny_app/global.R +++ b/shiny_app/global.R @@ -11,7 +11,7 @@ library(shiny) library(shinymanager) library(phsstyles) #chart colours -data_allergy <- readRDS(paste0(filepath, "allergy_scotland_chart.rds")) +data_allergy <- readRDS("allergy_scotland_chart.rds") #Use for selection of conditions condition_list <- sort(unique(data_allergy$type)) diff --git a/shiny_app/server.R b/shiny_app/server.R index eb987de..5dd0b4e 100644 --- a/shiny_app/server.R +++ b/shiny_app/server.R @@ -1,7 +1,7 @@ ############################. ## Server ---- ############################. -#credentials_allergy <- readRDS("credentials.rds") +credentials_allergy <- readRDS("admin/credentials.rds") function(input, output) { diff --git a/shiny_app/ui.R b/shiny_app/ui.R index a0df58f..c717c2f 100644 --- a/shiny_app/ui.R +++ b/shiny_app/ui.R @@ -3,7 +3,7 @@ ############################. #Height and widths as percentages to allow responsiveness #Using divs as issues with classing css -#secure_app( +secure_app( fluidPage(style="width: 650px; height: 500px; ", div(style= "width:100%", #Filters on top of page h4("Chart 1. Hospital admissions for different allergic conditions"), @@ -31,4 +31,4 @@ fluidPage(style="width: 650px; height: 500px; ", ) ) )#fluid page bracket -#) \ No newline at end of file +) \ No newline at end of file From 08b4c931fe5138c1d5177045794b0216a0f26358 Mon Sep 17 00:00:00 2001 From: apigap01 Date: Thu, 28 Nov 2024 16:52:30 +0000 Subject: [PATCH 3/4] Data source link fix --- shiny_app/ui.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shiny_app/ui.R b/shiny_app/ui.R index c717c2f..eaaadfe 100644 --- a/shiny_app/ui.R +++ b/shiny_app/ui.R @@ -20,7 +20,7 @@ fluidPage(style="width: 650px; height: 500px; ", #plotlyOutput("chart", width = "100%", height = "350px"), highchartOutput("line_chart"), p(div(style = "width: 25%; float: left;", #Footer - HTML("Source: PHS, SMR 01")), + HTML("Source: PHS, SMR 01")), div(style = "width: 25%; float: left;", downloadLink('download_data', 'Download data')), div(style = "width: 100%; float: left;", From 8af279911ffe17e779cce0f0f0d302e518fde994 Mon Sep 17 00:00:00 2001 From: apigap01 Date: Tue, 17 Dec 2024 10:30:35 +0000 Subject: [PATCH 4/4] Commenting out password protection as PRA now complete --- shiny_app/server.R | 18 +++++++++--------- shiny_app/ui.R | 4 ++-- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/shiny_app/server.R b/shiny_app/server.R index 5dd0b4e..9237dec 100644 --- a/shiny_app/server.R +++ b/shiny_app/server.R @@ -1,18 +1,18 @@ ############################. ## Server ---- ############################. -credentials_allergy <- readRDS("admin/credentials.rds") +#credentials_allergy <- readRDS("admin/credentials.rds") function(input, output) { - # Shinymanager Auth - res_auth <- secure_server( - check_credentials = check_credentials(credentials_allergy) - ) - - output$auth_output <- renderPrint({ - reactiveValuesToList(res_auth) - }) + # # Shinymanager Auth + # res_auth <- secure_server( + # check_credentials = check_credentials(credentials_allergy) + # ) + # + # output$auth_output <- renderPrint({ + # reactiveValuesToList(res_auth) + # }) # Allowing user to download data output$download_data <- downloadHandler( diff --git a/shiny_app/ui.R b/shiny_app/ui.R index eaaadfe..5288a14 100644 --- a/shiny_app/ui.R +++ b/shiny_app/ui.R @@ -3,7 +3,7 @@ ############################. #Height and widths as percentages to allow responsiveness #Using divs as issues with classing css -secure_app( +#secure_app( fluidPage(style="width: 650px; height: 500px; ", div(style= "width:100%", #Filters on top of page h4("Chart 1. Hospital admissions for different allergic conditions"), @@ -31,4 +31,4 @@ fluidPage(style="width: 650px; height: 500px; ", ) ) )#fluid page bracket -) \ No newline at end of file +#) \ No newline at end of file