No feedback found for this session
Scope of the possible with R
R
overview
Session materials
- all materials
- slides
html / pdf
Slides for this session / .pdf slides 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
Load that data
library(readr)
<- read_csv("data/weekly_ae_activity_20240609.csv") ae_activity
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
date | country | hb | loc | type | attend | n_within | n_4 | perc_4 | n_8 | perc_8 | n_12 | perc_12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
20151115 | S92000003 | S08000028 | W107H | Emergency Department | 107 | 107 | 0 | 100.0 | 0 | 0.0 | 0 | 0.0 |
20190707 | S92000003 | S08000020 | N101H | Emergency Department | 1242 | 1073 | 169 | 86.4 | 5 | 0.4 | 0 | 0.0 |
20190602 | S92000003 | S08000025 | R103H | Emergency Department | 122 | 116 | 6 | 95.1 | 1 | 0.8 | 0 | 0.0 |
20200802 | S92000003 | S08000032 | L308H | Emergency Department | 1275 | 1132 | 143 | 88.8 | 16 | 1.3 | 1 | 0.1 |
20240512 | S92000003 | S08000015 | A111H | Emergency Department | 1292 | 813 | 479 | 62.9 | 186 | 14.4 | 99 | 7.7 |
Removing data
<- ae_activity |>
ae_activity select(!c(country, contains("perc_")))
date | hb | loc | type | attend | n_within | n_4 | n_8 | n_12 |
---|---|---|---|---|---|---|---|---|
20200927 | S08000024 | S319H | Emergency Department | 858 | 837 | 21 | 0 | 0 |
20181014 | S08000020 | N121H | Emergency Department | 330 | 325 | 5 | 0 | 0 |
20160821 | S08000017 | Y144H | Emergency Department | 238 | 233 | 5 | 0 | 0 |
20220619 | S08000031 | G405H | Emergency Department | 1706 | 723 | 983 | 254 | 41 |
20151025 | S08000030 | T101H | Emergency Department | 850 | 839 | 11 | 0 | 0 |
Tidying data
<- ae_activity |>
ae_activity mutate(date = lubridate::ymd(date))
date | hb | loc | type | attend | n_within | n_4 | n_8 | n_12 |
---|---|---|---|---|---|---|---|---|
2024-05-26 | S08000020 | N101H | Emergency Department | 1050 | 475 | 575 | 248 | 88 |
2016-08-14 | S08000016 | B120H | Emergency Department | 538 | 510 | 28 | 0 | 0 |
2015-05-17 | S08000020 | N101H | Emergency Department | 1074 | 1038 | 36 | 3 | 0 |
2019-02-24 | S08000031 | G405H | Emergency Department | 1947 | 1487 | 460 | 41 | 2 |
2016-10-30 | S08000017 | Y146H | Emergency Department | 634 | 616 | 18 | 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)
date | hb | loc | type | attend | n_within | n_4 | n_8 | n_12 |
---|---|---|---|---|---|---|---|---|
2022-06-05 | S08000030 | T202H | Emergency Department | 541 | 492 | 49 | 3 | 0 |
2021-02-28 | S08000022 | C121H | Emergency Department | 81 | 81 | 0 | 0 | 0 |
2018-11-04 | S08000022 | H103H | Emergency Department | 155 | 152 | 3 | 0 | 0 |
2017-07-02 | S08000032 | L106H | Emergency Department | 1235 | 1208 | 27 | 2 | 0 |
2018-09-23 | S08000017 | Y146H | Emergency Department | 671 | 605 | 66 | 2 | 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::addTiles() |>
leaflet::addMarkers(~longitude, ~latitude, label = ~HospitalName) leaflet
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::addTiles() |>
leaflet::addMarkers(~longitude, ~latitude, label = ~rate) leaflet
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