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

Example map initial PR #110

Open
wants to merge 2 commits into
base: main
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
9 changes: 9 additions & 0 deletions R/read_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,12 @@ read_revenue_data <- function(file = "data/la_maintained_schools_revenue_reserve
)
return(df_revenue)
}

# map data ----------------------------------------------------------------
read_upper_tier_data <- function(file = "data/Upper_Tier_Local_Authorities_Simplified.geojson") {
# This reads in an example file. For the purposes of this demo, we're using
# the LA expenditure data downloaded from an EES release
df_upper_tier <- read_sf(file)

return(df_upper_tier)
}
45 changes: 45 additions & 0 deletions R/ui_panels/example_tab_1.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,51 @@ example_tab_1_panel <- function() {
)
)
),
# Map tab --------------------------------------------------
tabPanel(
"Map example",
fluidRow(
column(
width = 12,
h2("An example map using leaflet"),
# map output here ---------------------------------------
column(
width = 6,
leafletOutput(
"mapOut"
)
),
column(
width = 6,
div(
class = "well",
style = "min-height: 100%; height: 100%; overflow-y:
visible",
fluidRow(
# Map dropdown selection ---------------------
column(
width = 12,
selectizeInput(
"selectMapYear",
"Select Year",
choices = df_revbal_years,
multiple = FALSE,
selected = max(df_revbal_years)
),
selectizeInput(
"selectMapPhase",
"Select School Phase",
choices = df_revbal_phase,
multiple = FALSE,
selected = "All Local authority maintained schools"
)
)
)
)
)
)
)
),
# Benchmarking tab ------------------------------------------------
tabPanel(
"Benchmarking example",
Expand Down
194 changes: 194 additions & 0 deletions data/Upper_Tier_Local_Authorities_Simplified.geojson

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions global.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ shhh(library(shinyGovstyle))
# Creating charts and tables
shhh(library(ggplot2))
shhh(library(DT))
shhh(library(leaflet))
shhh(library(htmltools))

# Data and string manipulation
shhh(library(dplyr))
Expand Down Expand Up @@ -91,6 +93,9 @@ enableBookmarking("url")
# Read in the data ------------------------------------------------------------
df_revbal <- read_revenue_data()

df_revbal_years <- sort(unique(df_revbal$year))
df_revbal_phase <- unique(df_revbal$school_phase)

# Get geographical areas from data
df_areas <- df_revbal %>%
select(
Expand All @@ -100,6 +105,17 @@ df_areas <- df_revbal %>%
) %>%
distinct()

df_upper_tier_geo <- read_upper_tier_data()

df_upper_tier_all <- df_upper_tier_geo %>%
dplyr::select(UTLA22NM, LONG, LAT, geometry) %>%
dplyr::rename("area_name" = "UTLA22NM") %>%
inner_join(df_revbal,
by = "area_name"
) %>%
rowwise() %>%
mutate(lab = HTML(sprintf("%s <hr> %s </br> %s %s", area_name, "Schools with deficit", PC_schools_with_deficit, "%")))

# Extract lists for use in drop downs -----------------------------------------
# LA list
choices_las <- df_areas %>%
Expand Down
2 changes: 1 addition & 1 deletion renv.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"Repositories": [
{
"Name": "CRAN",
"URL": "https://packagemanager.posit.co/cran/latest"
"URL": "http://cran.rstudio.com/"
}
]
},
Expand Down
54 changes: 54 additions & 0 deletions server.R
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,55 @@ server <- function(input, output, session) {
)
})

# Dataset with map data ----------------------------------------------
reactive_map_dataset <- reactive({
df_upper_tier_all %>%
dplyr::filter(
year == input$selectMapYear,
area_name != "England",
school_phase == input$selectMapPhase
) %>%
dplyr::select(
area_name,
PC_schools_with_deficit,
LONG,
LAT,
geometry,
lab
)
})

reactive_map_pal <- reactive({
quantile_num <- 5
probs <- seq(0, 1, length.out = quantile_num + 1)
bins <- quantile(reactive_map_dataset()$PC_schools_with_deficit, probs, na.rm = TRUE, names = FALSE)
bins <- unique(bins)

pal <- colorBin("YlOrRd", bins = bins)
return(pal)
})

reactive_map_to_display <- reactive({
leaflet(reactive_map_dataset()) %>%
addTiles() %>%
setView(lng = -3.95, lat = 53, zoom = 5.5) %>%
addPolygons(
color = "black",
fillColor = ~ reactive_map_pal()(PC_schools_with_deficit),
fillOpacity = 0.5,
stroke = TRUE,
weight = 0.2,
opacity = 0.8,
label = ~lab
) %>%
addLegend(
# "topright",
pal = reactive_map_pal(), values = ~PC_schools_with_deficit,
title = "% Schools with Deficit",
opacity = 1
)
})

# Dataset with benchmark data -----------------------------------------------
reactive_benchmark <- reactive({
df_revbal %>%
Expand All @@ -194,6 +243,11 @@ server <- function(input, output, session) {
)
})

output$mapOut <- renderLeaflet({
reactive_map_to_display()
# already_there
})

# Benchmarking bar chart
output$colBenchmark <- renderGirafe({
girafe(
Expand Down
37 changes: 37 additions & 0 deletions www/dfe_shiny_gov_style.css
Original file line number Diff line number Diff line change
Expand Up @@ -409,4 +409,41 @@ html {
.small-box.bg-dark-blue {
background-color: #12436D !important;
color: #ffffff !important;
}


/* Map formatting */
@import '~govuk-frontend/dist/govuk/base';

.map {
background-color: govuk-colour('light-grey');

path:focus {
outline: none;
}
}

.tooltip {
white-space: normal;
width: 100%;
}

.tooltipContent {
font-size: 0.9rem;
margin-bottom: 0;
}

.legend {
align-items: center;
display: flex;
line-height: 20px;
margin-bottom: 0;
}

.legendIcon {
border: 1px solid govuk-colour('black');
forced-color-adjust: none;
height: 20px;
margin-right: govuk-spacing(1);
width: 20px;
}
Loading