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.

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 Improving Performance by Hadley Wickham.

Developers#

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

License Info#

from IPython.display import Markdown, display

def display_markdown(file_path):
    try:
        with open(file_path, 'r') as file:
            content = file.read()
            display(Markdown(content))
    except FileNotFoundError:
        print(f"The file {file_path} was not found.")
    except Exception as e:
        print(f"An error occurred: {e}")

# Example usage:
file_path = '../individual_modules/improve_your_r_code/LICENSE.txt'  # Replace with your file path
display_markdown(file_path)

Instructional Material

The instructional material in this course is copyright © 2024 University of Exeter and is made available under the Creative Commons Attribution 4.0 International licence (https://creativecommons.org/licenses/by/4.0/). Instructional material consists of material that is contained within the “individual_modules/improve_your_r_code” directory, and images folders in this directory, with the exception of code snippets and example programs found in files within these folders. Such code snippets and example programs are considered software for the purposes of this licence.

Software

Except where otherwise noted, software provided in this repository is made available under the MIT licence (https://opensource.org/licenses/MIT).

Copyright © 2024 University of Exeter

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

The software in this repository is adapted from software that is covered by the following copyright and permission notice:

Copyright © 2024 Software Carpentry

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.