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
One small bit of cheating: renaming
Preview
date | country | hb | loc | type | attend | n_within | n_4 | perc_4 | n_8 | perc_8 | n_12 | perc_12 |
---|---|---|---|---|---|---|---|---|---|---|---|---|
20200419 | S92000003 | S08000019 | V217H | Emergency Department | 580 | 559 | 21 | 96.4 | 1 | 0.2 | 0 | 0.0 |
20200607 | S92000003 | S08000030 | T101H | Emergency Department | 752 | 741 | 11 | 98.5 | 0 | 0.0 | 0 | 0.0 |
20150510 | S92000003 | S08000030 | T101H | Emergency Department | 896 | 885 | 11 | 98.8 | 0 | 0.0 | 0 | 0.0 |
20200405 | S92000003 | S08000022 | H212H | Emergency Department | 38 | 36 | 2 | 94.7 | 0 | 0.0 | 0 | 0.0 |
20240211 | S92000003 | S08000022 | H212H | Emergency Department | 142 | 112 | 30 | 78.9 | 5 | 3.5 | 1 | 0.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 |
---|---|---|---|---|---|---|---|---|
20201213 | S08000025 | R103H | Emergency Department | 88 | 87 | 1 | 0 | 0 |
20201206 | S08000024 | S319H | Emergency Department | 710 | 684 | 26 | 0 | 0 |
20220605 | S08000026 | Z102H | Emergency Department | 173 | 166 | 7 | 0 | 0 |
20180603 | S08000015 | A210H | Emergency Department | 865 | 837 | 28 | 1 | 0 |
20230604 | S08000031 | C418H | Emergency Department | 1224 | 829 | 395 | 102 | 5 |
Tidying data
ae_activity <- ae_activity |>
mutate(date = lubridate::ymd(date))
date | hb | loc | type | attend | n_within | n_4 | n_8 | n_12 |
---|---|---|---|---|---|---|---|---|
2016-10-16 | S08000031 | G513H | Emergency Department | 1128 | 1126 | 2 | 0 | 0 |
2016-08-28 | S08000022 | H103H | Emergency Department | 164 | 157 | 7 | 1 | 0 |
2022-01-23 | S08000026 | Z102H | Emergency Department | 110 | 107 | 3 | 0 | 0 |
2022-08-07 | S08000032 | L302H | Emergency Department | 1120 | 704 | 416 | 83 | 10 |
2022-05-29 | S08000030 | T202H | Emergency Department | 465 | 434 | 31 | 1 | 0 |
Subset data
- we’ll take a random selection of 5 health boards to keep things tidy
date | hb | loc | type | attend | n_within | n_4 | n_8 | n_12 |
---|---|---|---|---|---|---|---|---|
2020-10-18 | S08000017 | Y146H | Emergency Department | 539 | 483 | 56 | 0 | 0 |
2021-11-07 | S08000017 | Y144H | Emergency Department | 197 | 178 | 19 | 2 | 0 |
2020-07-19 | S08000020 | N411H | Emergency Department | 504 | 458 | 46 | 5 | 1 |
2022-03-27 | S08000029 | F704H | Emergency Department | 1306 | 955 | 351 | 102 | 25 |
2020-03-22 | S08000015 | A111H | Emergency Department | 964 | 937 | 27 | 1 | 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