Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update with 22/23 data and convert chart from plotly to highcharter #1

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand All @@ -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]
Expand Down
12 changes: 7 additions & 5 deletions data_preparation.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
## END
8 changes: 2 additions & 6 deletions shiny_app/global.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

#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
47 changes: 20 additions & 27 deletions shiny_app/server.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
############################.
## Server ----
############################.
credentials_allergy <- readRDS("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(
Expand All @@ -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 <br>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
} # end of server part
11 changes: 6 additions & 5 deletions shiny_app/ui.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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: <a href='https://beta.isdscotland.org/find-publications-and-data/health-services/hospital-care/'>PHS, SMR 01</a>")),
HTML("Source: <a href='https://publichealthscotland.scot/resources-and-tools/health-intelligence-and-data-management/national-data-catalogue/national-datasets/search-the-datasets/general-acute-inpatient-and-day-case-scottish-morbidity-record-smr01/' target='_blank'>PHS, SMR 01</a>")),
div(style = "width: 25%; float: left;",
downloadLink('download_data', 'Download data')),
div(style = "width: 100%; float: left;",
Expand All @@ -30,4 +31,4 @@ fluidPage(style="width: 650px; height: 500px; ",
)
)
)#fluid page bracket
)
#)