More interactive, more engaging

Teaching R in a lecture environment

RSS International Conference 2024
Nicola Rennie

About me

Lecturer in Health Data Science within Centre for Health Informatics, Computing, and Statistics.


Academic background in statistics, with experience in data science consultancy.


Teaching students with mathematical and non-mathematical backgrounds, at a variety of levels, in both academia and industry.

CHICAS logo

Teaching statistics with R

  • Many introductory (and more advanced) statistics courses also include instruction on using R.

  • Many courses teach statistical concepts in lectures, then the practical aspects of using R in separate workshops with smaller groups.

  • Including programming examples in a lecture environment isn’t always easy (for instructors and students).

R logo

Screenshots of RStudio

Screenshot of RStudio showing code and map

Screenshots of code

Screenshot of RStudio showing code

Including code blocks

# read in data
census <- readxl::read_xlsx("Data/Census2021-health-la.xlsx", skip = 4)

# get local authority map shapefiles
la_map <- sf::st_read("Data/Local_Authority_Districts_December_2021/LAD_DEC_2021_GB_BGC.shp") 

# calculate percentage that have bad/very bad
bad_health <- census |> 
  mutate(Percentage = `Bad health \r\n(age-standardised \r\nproportions)` + 
           `Very bad health \r\n(age-standardised \r\nproportions)`) |> 
  select(`Area code`, Percentage) 

# highest / lowest percentage
slice_max(bad_health, Percentage, n = 1)
slice_min(bad_health, Percentage, n = 1)
round(mean(bad_health$Percentage), 1)

Live coding!

Pros

  • shows the process of writing code

  • teaches students how to debug code

  • demonstrates good programming practices

Cons

  • Switching between windows

  • More pressure as a demonstrator

  • Hard for students to run the code themselves

Live coding!


How do we make live coding easier (and better)?


with webR!

webR logo

What is webR?

webR is a version of R that runs in a web browser.

  • You don’t need to install R, or create a cloud account.

  • You don’t need to set up a server.

  • It just works (even on your phone).

webR logo

Using webR for teaching

  • Interactive code blocks in slides

  • Interactive R console from slides

  • Deploying Shiny apps

  • Quarto Live

Gif of Quarto drop

But first, Quarto…

Quarto is a way to combine code with text to create a variety of output formats. Essentially, second generation R Markdown that also natively supports other programming languages.


Create HTML slides:

---
title: "Lecture 1"
format: revealjs
---

Adding code blocks with Quarto

```{r}
# set a random seed and generate data
set.seed(123)
x <- rnorm(100)

# calculate standard deviation
sd(x)
```


# set a random seed and generate data
set.seed(123)
x <- rnorm(100)

# calculate standard deviation
sd(x)

Interactive code blocks

Interactive code blocks

Install the Quarto webR extension:

quarto add coatless/quarto-webr


Add the webR filter to slides:

format: revealjs
filters:
  - webr


Add a webR code block:

```{webr-r}
# set a random seed and generate data
set.seed(123)
x <- rnorm(100)

# calculate mean
mean(x)
```

Interactive console

Gif of Quarto drop

Interactive console

  • Press the “`” key to make the console appear/disappear. Alternatively, press the console button at the bottom left of the slide.

  • You can load packages automatically.

  • The state is maintained over multiple slides.

Interactive code blocks

Install the Quarto Drop plugin:

quarto add r-wasm/quarto-drop


Add the drop plugin to slides:

format: revealjs
revealjs-plugins:
  - drop

Advantages and disadvantages

Advantages

  • Write and run code without switching between slides and RStudio.

  • Students can engage with code in lectures on their phones.

  • No set up required for students (and easy to fix mistakes!)

  • Use code blocks that are empty, have comments, or have pre-filled code.

Limitations

  • Requires an internet connection.

  • Not a replacement for workshops. Teaching R/RStudio/package installation is still important.

Resources