Pete Lawson
  • Home
  • Posts
  • Curriculum Vitae

On this page

  • RStudio 1.3 - A New Hope
  • Building a Theme Switcher
    • Step 1: Creating Distinct Configurations
    • Step 2: Create a bash script for swapping between active configurations
    • Step 3: Integrate the bash script with Alfred
  • Conclusion

Building a theme switcher for teaching with RStudio

RStudio
Teaching
Alfred
Switch quickly between a vanilla RStudio configuration for teaching, and a custom development configuration.
Author
Affiliation

Pete Lawson

Data Services @ Johns Hopkins University

Published

July 1, 2024

Part of my day to day involves teaching workshops in R and Python. For my R workshops I teach with RStudio. I rely heavily on participatory live coding. It is important to keep your configuration as close to your learners as possible, which means sticking with the vanilla, default settings for the applications you are teaching with.

I also use RStudio for development (although that might change with the introduction of Positron), and have a bespoke configuration, with VIM bindings, custom panel layout, custom color scheme, and custom snippets.

It is a pain to reset my configuration to default, and I can never remember what the default pane layout is, and lastly I don’t want to lose all of my custom configuration.

A teaching mode is an open RStudio issue (since 2019): see [Feature request] Teaching/presentation mode #4276 and the related issue Feature Request: Provide Session-level control over Themes or Background Colors #2350.

In fact, my comment on #2350 seemd to push the issue up from backlog3 to backlog2, but it doesn’t seem like any work was done on it since then.

RStudio 1.3 - A New Hope

With the introduction of RStudio 1.3, your configuration is stored as a json (rstudio-prefs.json) in the rstudio folder within .config in your home directory.

This gave me an idea - what if I could keep two copies of my configuration; one for teaching and one for development, and write a bash script to swap between them?

Building a Theme Switcher

Step 1: Creating Distinct Configurations

  1. Close RStudio

  2. Rename the existing configuration folder to preserve your development configuration:

    mv ~/.config/rstudio ~/.config/rstudiodev
  3. Launch RStudio. It will generate a new .config/rstudio folder with default, vanilla settings.

Now, ~/.config/rstudiodev contains the development configuration, and ~/.config/rstudio contains the teaching configuration. RStudio uses whichever folder is named rstudio as the active configuration.

Step 2: Create a bash script for swapping between active configurations

I wrote a simple script to check which directory is present, either rstudiodev or rstudioteach. If rstudioteach is present, then that means that rstudio is the development config. The script will rename rstudio to rstudiodev, and rename rstudioteach to rstudio, thus swapping the development and teaching configurations. If I call the script again, the reverse will occur:

dev_config="$HOME/.config/rstudiodev"
teach_config="$HOME/.config/rstudioteach"
active_config="$HOME/.config/rstudio"

if [ -d "$teach_config" ]; then
  mv "$active_config" "$dev_config"
  mv "$teach_config" "$active_config"
  echo "Teaching mode active!"
elif [ -d "$dev_config" ]; then
  mv "$active_config" "$teach_config"
  mv "$dev_config" "$active_config"
  echo "Development mode active!"
fi

Step 3: Integrate the bash script with Alfred

To streamline the process further, I integrated this script with Alfred, a powerful application launcher for macOS.

By setting up a custom Alfred workflow, I can now switch between teaching and development configurations with a simple keyboard shortcut or voice command.

  1. Create a new workflow from template:

    Select Templates > Essentials > Keyword to Script Notification

    Keyword to Script Notification allows us to activate the script with a keyword, and generate a notification with the active configuration.

  2. Configure the workflow:

    1. Set Keyword:

    1. Configure Script:

    1. Set Notification:

Now when you activate teach in Alfred:

Now you can toggle between teaching and development mode!

Conclusion

This solution offers a quick and efficient way to toggle between teaching and development environments in RStudio. While it’s not as seamless as a built-in teaching mode, it significantly reduces the friction of switching between configurations, allowing me to maintain my preferred setup for development while easily adapting to a learner-friendly environment for workshops.

Citation

BibTeX citation:
@online{lawson2024,
  author = {Lawson, Pete},
  title = {Building a Theme Switcher for Teaching with {RStudio}},
  date = {2024-07-01},
  url = {https://petelawson.com/posts/2024-07-01-rstudio-automatic-theming/},
  langid = {en}
}
For attribution, please cite this work as:
Lawson, Pete. 2024. “Building a Theme Switcher for Teaching with RStudio.” July 1, 2024. https://petelawson.com/posts/2024-07-01-rstudio-automatic-theming/.

Copyright 2025, Pete Lawson

 

This website was built in with Quarto & deployed with Netlify