Improve Your R Code#

Course Description#

R is primarily known as a language and environment for statistical computing and graphics. However, its flexibility and accessibility have led to widespread use across disciplines including biosciences, medicine, and social sciences. This workshop is designed to help learners improve the style and speed of their R code. It is aimed at individuals who already write R scripts and want to develop more maintainable and performant code.

Course Objectives#

By the end of this workshop, you will:

  • Be able to write clean, readable, and maintainable R code following the tidyverse style guide.

  • Be able to measure and benchmark code performance using microbenchmark.

  • Understand and apply tools such as parallel, data.table, and Rcpp to improve execution speed.

  • Use styler to automatically format and clean your code.

  • Understand when optimisation is appropriate and how to identify performance bottlenecks.

Pre-requisite Knowledge#

This course will not include an introduction to R, or how to setup and use R or Rstudio. It is assumed you are comfortable coding in R and are familiar with:

  • Writing and executing commands in the R console.

  • Writing functions in R.

If not we recommend that you consult our pre-requisite course Introduction to R.

The interactive network visualisation below displays the prerequisite structure for this course within the training program. Each node represents a course that you may need to complete beforehand, and the arrows show the recommended order in which to take them, leading up to your selected course. You can click on any course node to view more information about that course. This interactive tool helps you clearly see the learning path required to access this course, making it easier to plan your progress with the Coding for Reproducible Research Training (CfRR) initiative.

Pre-Reqs Subnetwork

Requirements#

Please install and load the following R packages:

data.table
dplyr
learnr
microbenchmark
parallel
parallelly
Rcpp
styler

learnr#

A interactive learnr tutorial can be downloaded by executing the following commands in R:

install.packages("devtools") 
install.packages("learnr") 
library(devtools)
library(learnr)
learnr::run_tutorial("Improve Your R Code", "cfrrRtutorials", clean = TRUE)

You can navigate through the sections using the menu on the side. The sections on speed are based on Measuring Performance and Advanced R: Performance Improvements by Hadley Wickham.

Developers#

This material was developed by Conor Crilly as part of the Coding for Reproducible Research programme.

License Info#

The file ../individual_modules/improve_your_r_code/LICENSE.txt was not found.