Teaching statistics interactively with webR

RSS International Conference 2023

Nicola Rennie

Teaching statistics with R

About me

Lecturer in Health Data Science at Lancaster Medical School.


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.


Blog about R and data science at nrennie.rbind.io/blog.

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.

Screenshots of RStudio

Screenshot of RStudio showing code and map

Screenshots of code

Screenshot of RStudio showing code

Code blocks with R Markdown (or Quarto!)

# 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

webR

What is webR?

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

  • You don’t need to install R.

  • You don’t need to setup a server.

  • It just works (even on your phone).

What can you do with webR?

Lots of things!

Screenshot of RStudio showing code

Try it!

nrennie.rbind.io/rss-2023-teaching-webR

Adding webR to teaching materials

  • Quarto
    • Combine code with text
    • Multiple output formats
    • Next generation R Markdown
  • HTML slides
  • Supports LaTeX

Quarto logo

Adding webR to teaching materials

Install the webR Quarto extension:

quarto add coatless/quarto-webr

Add the webR filter to slides:

format: revealjs
filters:
  - webr

Adding webR to teaching materials

Add a webR code block:

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

# calculate mean
mean(x)
```

Advantages of webR

  • 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 of webR

webr::install("dplyr")
  • Not a replacement for workshops. Teaching R/RStudio/package installation is still important.

Resources

Questions?