Summary and Schedule
In this lesson you will learn what a reproducible example is and how it can help you better understand your code and solve error messages and other issues that may arise.
- Minimal Reproducible Examples: what are they and why are they useful?
- Understanding Your Code: what is each line in my code actually doing?
- De-bugging Basics: what do I do when I encounter an error?
- Minimal Reproducible Data: What data do I use? How do I create dummy data?
- Minimal Reproducible Code: Which parts of my code do I actually need to replicate?
- Asking Your Question: My code is ready, how do I ask my question?
Setup Instructions | Download files required for the lesson | |
Duration: 00h 00m | 1. What is a reprex and why is it useful? |
How is the process of getting help in R different from getting help with
other things? Why is a minimal reproducible example an important tool for getting help in R? What will we be learning in the rest of the course? |
Duration: 00h 17m | 2. Identify the problem and make a plan |
What do I do when I encounter an error? What do I do when my code outputs something I don’t expect? Why do errors and warnings appear in R? Which areas of code are responsible for errors? How can I fix my code? What other options exist if I can’t fix it? |
Duration: 00h 17m | 3. Minimal Reproducible Data |
What is a minimal reproducible dataset, and why do I need it? What do I need to include in a minimal reproducible dataset? How do I create a minimal reproducible dataset? How do I make my own dataset reproducible? |
Duration: 01h 52m | 4. Minimal Reproducible Code |
Why can’t I just post my whole script? Which parts of my code are directly relevant to my problem? Which parts of my code are necessary in order for the problem area to run correctly? I feel overwhelmed by this script–where do I even start? ::: |
Duration: 03h 07m | 5. Asking your question |
How can I make sure my minimal reproducible example will actually run
correctly for someone else? How can I easily share a reproducible example with a mentor or helper, or online? How do I ask a good question? |
Duration: 03h 19m | Finish |
The actual schedule may vary slightly depending on the topics and exercises chosen by the instructor.
Setup instructions live in this document. Please specify the tools
and the data sets the learner needs to have installed. If you want to
hide different setup instructions, you can use a solution
tag.
Install R and RStudio
R and RStudio are two separate pieces of software:
- R is a programming language and software used to run code written in R.
- RStudio is an integrated development environment (IDE) that makes using R easier. In this course we use RStudio to interact with R.
If you don’t already have R and RStudio installed, follow the instructions for your operating system below. You have to install R before you install RStudio.
- Download R from the CRAN website.
- Run the
.exe
file that was just downloaded - Go to the RStudio download page
- Under Installers select RStudio x.yy.zzz - Windows Vista/7/8/10 (where x, y, and z represent version numbers)
- Double click the file to install it
- Once it’s installed, open RStudio to make sure it works and you don’t get any error messages.
- Download R from the CRAN website.
- Select the
.pkg
file for the latest R version - Double click on the downloaded file to install R
- It is also a good idea to install XQuartz (needed by some packages)
- Go to the RStudio download page
- Under Installers select RStudio x.yy.zzz - Mac OS X 10.6+ (64-bit) (where x, y, and z represent version numbers)
- Double click the file to install RStudio
- Once it’s installed, open RStudio to make sure it works and you don’t get any error messages.
- Download R from the CRAN website.
- Select the
.pkg
file for the latest R version - Double click on the downloaded file to install R
- It is also a good idea to install XQuartz (needed by some packages)
- Go to the RStudio download page
- Under Installers select RStudio x.yy.zzz - Mac OS X 10.6+ (64-bit) (where x, y, and z represent version numbers)
- Double click the file to install RStudio
- Once it’s installed, open RStudio to make sure it works and you don’t get any error messages.
Update R and RStudio
If you already have R and RStudio installed, first check if your R version is up to date:
- When you open RStudio your R version will be printed in the console
on the bottom left. Alternatively, you can type
sessionInfo()
into the console. If your R version is 4.0.0 or later, you don’t need to update R for this lesson. If your version of R is older than that, download and install the latest version of R from the R project website for Windows, for MacOS, or for Linux - It is not necessary to remove old versions of R from your system, but if you wish to do so you can check How do I uninstall R?
- After installing a new version of R, you will have to reinstall all
your packages with the new version. For Windows, there is a package
called
installr
that can help you with upgrading your R version and migrate your package library. A similar package calledpacman
can help with updating R packages across To update RStudio to the latest version, open RStudio and click onHelp > Check for Updates
. If a new version is available follow the instruction on screen. By default, RStudio will also automatically notify you of new versions every once in a while.
Callout
The changes introduced by new R versions are usually
backwards-compatible. That is, your old code should still work after
updating your R version. However, if breaking changes happen, it is
useful to know that you can have multiple versions of R installed in
parallel and that you can switch between them in RStudio by going to
Tools > Global Options > General > Basic
.
While this may sound scary, it is far more common to run into issues due to using out-of-date versions of R or R packages. Keeping up with the latest versions of R, RStudio, and any packages you regularly use is a good practice.
Install required R packages
During the course we will need a number of R packages. Packages
contain useful R code written by other people. We will use the packages
ratdat
, reprex
, dplyr
, and
ggplot2
.
To try to install these packages, open RStudio and copy and paste the following command into the console window (look for a blinking cursor on the bottom left), then press the Enter (Windows and Linux) or Return (MacOS) to execute the command.
{r} install.packages(c("ratdat", "reprex", "dplyr", "ggplot2"))
Alternatively, you can install the packages using RStudio’s graphical
user interface by going to Tools > Install Packages
and
typing the names of the packages separated by a comma.
R tries to download and install the packages on your machine.
When the installation has finished, you can try to load the packages by pasting the following code into the console:
R
library(ratdat)
library(reprex)
library(dplyr)
library(ggplot2)
If you do not see an error like
there is no package called ‘...’
you are good to go!
Updating R packages
Generally, it is recommended to keep your R version and all packages
up to date, because new versions bring improvements and important
bugfixes. To update the packages that you have installed, click
Update
in the Packages
tab in the bottom right
panel of RStudio, or go to
Tools > Check for Package Updates...
You should update all of the packages required for the lesson, even if you installed them relatively recently.
Sometimes, package updates introduce changes that break your old
code, which can be very frustrating. To avoid this problem, you can use
a package called renv
. It locks the package versions you
have used for a given project and makes it straightforward to reinstall
those exact package version in a new environment, for example after
updating your R version or on another computer. However, the details are
outside of the scope of this lesson.