In this computer assessment you will build a single Shiny app containing two interactive visualisations of Northern Ireland census geography data. The content assessed is:
read_sf()geom_sf()fill
aestheticselectInput in conjunction with filter() from the package dplyr to make both
visualisations react to a single user inputTotal marks: 10
Ensure the following packages are installed, installing any that are missing:
install.packages(c("shiny", "ggplot2", "dplyr", "sf"))
Load them at the top of your app.R:
library(shiny)
library(ggplot2)
library(dplyr)
library(sf)
app.RIn RStudio, go to File → New File → Shiny Web App. Set the app name to
computer-assessment and choose Single File (app.R). RStudio will create
a new folder called computer-assessment/ in your working directory containing
app.R. All your work for this assessment goes inside that app.R.
The two visualisations required by Questions 1 and 2 must both live in this
single app, sharing the same input control. Place the two plots side by
side using fluidRow() and column() (each column with width = 6).
Download the Northern Ireland Super Data Zone (SDZ) boundaries from NISRA at
https://www.nisra.gov.uk/publications/super-data-zone-boundaries-gis-format
(choose the ESRI Shapefile option). Extract the downloaded zip file directly
into your computer-assessment/ folder, so that the path
computer-assessment/geography-sdz2021-esri-shapefile/SDZ2021.shp exists.
At the top of app.R, above the ui and server definitions, add the
following line to load the data once when the app starts:
ni <- read_sf("geography-sdz2021-esri-shapefile/SDZ2021.shp")
The object ni is a simple feature collection with 850 rows (one per Super
Data Zone) and the following columns relevant to this assessment:
| Column | Description |
|---|---|
SDZ2021_cd |
SDZ code |
SDZ2021_nm |
SDZ name |
LGD2014_nm |
Local Government District name (11 districts) |
Area_ha |
Area in hectares |
Perim_km |
Perimeter in kilometres |
Add a selectInput to the UI that lets the user choose one of the 11 Local
Government Districts from LGD2014_nm. Use the unique values of ni$LGD2014_nm
as the choices.
In the server, create a renderPlot() that produces a map with two layers:
geom_sf(data = ni).fill aesthetic according to the variable Perim_km and the boundaries not shown, meaning that you will have aes(fill = Perim_km) and colour = NA somewhere in the code.For reference, if the selected Local Government District is “Belfast”, your map should look like this:
Using the same selectInput from Question 1, add a second renderPlot()
to the server that produces a scatterplot of Area_ha (y-axis) against
Perim_km (x-axis) for the Super Data Zones belonging to the selected
Local Government District only.
For reference, if the selected Local Government District is “Belfast”, your map should look like this:
Add meaningful labels and titles to both plots. Although this question
carries no marks, clear labels are expected in professional data visualisation.
Use labs() to set x, y, fill and title for each plot. The title could, for
example, display the currently selected district name.