Scope of the possible with R

R
overview
Published

June 20, 2025

Session materials

Slides for this session / .pdf slides for this session

No feedback found for this session

Welcome

  • this session is a non-technical overview designed for service leads

Session outline

  • Why R, and why this session?
  • R demo - take some data, load, tidy, analyse
  • Strengths and weaknesses
    • obvious
    • less obvious
  • Alternatives
  • Skill development

R

  • free and open-source
  • multi-platform
  • large user base
  • prominent in health, industry, biosciences

Why this session?

  • R can be confusing
    • it’s code-based, and most of us don’t have much code experience
    • it’s used for some inherently complicated tasks
    • it’s a big product with lots of add-ons and oddities
  • But R is probably the best general-purpose toolbox we have for data work at present
    • big user base in health and social care
    • focus on health and care-like applications
    • not that hard to learn
    • extensible and flexible
    • capable of enterprise-y, fancy uses

R demo

  • this is about showing what’s possible, and give you a flavour of how R works
  • we won’t explain code in detail during this session
  • using live open data https://www.opendata.nhs.scot/dataset/weekly-accident-and-emergency-activity-and-waiting-times

Load that data

library(readr)
ae_activity <- read_csv("data/weekly_ae_activity_20240609.csv")

One small bit of cheating: renaming

names(ae_activity) <- c("date", "country", "hb", "loc", "type", "attend", "n_within", "n_4", "perc_4", "n_8", "perc_8", "n_12", "perc_12")

Preview

Preview of data
date country hb loc type attend n_within n_4 perc_4 n_8 perc_8 n_12 perc_12
20211003 S92000003 S08000016 B120H Emergency Department 511 421 90 82.4 12 2.3 5 1
20190414 S92000003 S08000019 V217H Emergency Department 1237 1091 146 88.2 4 0.3 0 0
20190526 S92000003 S08000031 C418H Emergency Department 1443 1280 163 88.7 17 1.2 0 0
20180211 S92000003 S08000022 H103H Emergency Department 139 134 5 96.4 1 0.7 0 0
20200719 S92000003 S08000024 S319H Emergency Department 743 731 12 98.4 1 0.1 0 0

Removing data

ae_activity <- ae_activity |>
    select(!c(country, contains("perc_")))
Preview of data
date hb loc type attend n_within n_4 n_8 n_12
20220320 S08000026 Z102H Emergency Department 116 116 0 0 0
20150329 S08000017 Y144H Emergency Department 265 255 10 1 0
20180805 S08000016 B120H Emergency Department 606 562 44 6 1
20170305 S08000032 L308H Emergency Department 1298 1042 256 33 5
20180812 S08000024 S308H Emergency Department 1122 974 148 14 2

Tidying data

ae_activity <- ae_activity |>
    mutate(date = lubridate::ymd(date))
Preview of data
date hb loc type attend n_within n_4 n_8 n_12
2022-03-06 S08000025 R103H Emergency Department 119 112 7 0 0
2021-10-10 S08000029 F704H Emergency Department 1208 855 353 99 9
2020-06-28 S08000031 C313H Emergency Department 417 404 13 0 0
2017-08-20 S08000022 C121H Emergency Department 161 160 1 0 0
2017-04-30 S08000031 C313H Emergency Department 658 632 26 0 0

Subset data

  • we’ll take a random selection of 5 health boards to keep things tidy
ae_activity <- ae_activity |>
    filter(hb %in% boards)
Preview of data
date hb loc type attend n_within n_4 n_8 n_12
2022-01-16 S08000015 A111H Emergency Department 871 658 213 119 91
2021-04-25 S08000015 A111H Emergency Department 1174 1084 90 17 6
2023-01-08 S08000030 T202H Emergency Department 418 381 37 1 0
2022-06-19 S08000026 Z102H Emergency Department 171 164 7 0 0
2016-02-28 S08000025 R103H Emergency Department 86 85 1 0 0

Basic plots

library(ggplot2)
ae_activity |>
    ggplot() +
    geom_line(aes(x = date, y = attend, colour = hb, group = loc)) 

Joining data

ae_activity |>
    left_join(read_csv("data/boards_data.csv"), by = c("hb" = "HB")) |>
    select(!any_of(c("_id", "HB", "HBDateEnacted", "HBDateArchived", "Country"))) |>
    ggplot() +
    geom_line(aes(x = date, y = attend, colour = HBName, group = loc))

and again…

Add to a map

ae_activity_loc |>
    leaflet::leaflet() |>
    leaflet::addTiles() |>
    leaflet::addMarkers(~longitude, ~latitude, label = ~HospitalName)

Then make that map more useful

ae_activity_loc |>
    group_by(HospitalName) |>
    summarise(attend = sum(attend), n_within = sum(n_within), longitude = min(longitude), latitude = min(latitude)) |>
    mutate(rate = paste(HospitalName, "averages", scales::percent(round(n_within / attend, 1)))) |>
    leaflet::leaflet() |>
    leaflet::addTiles() |>
    leaflet::addMarkers(~longitude, ~latitude, label = ~rate)

Then add to reports, dashboards…

Strengths

  • enormous scope and flexibility
  • a force-multiplier for fancier data work
    • helps collaboration within teams, between teams, between orgs
    • reproducible analytics
    • modular approaches to large projects
  • decreasing pain curve: the fancier the project, the better

Weaknesses

  • harder to learn than competitors
  • very patchy expertise across H+SC Scotland
  • complex IG landscape
  • messy skills development journey