Setup R and RStudio

Following along with tutorials in

Learn how to setup R and RStudio and follow along with the eReefs tutorials.

R and RStudio setup

Download and install R (the programming language) and RStudio (the integrated development environment) following these RStudio installation instructions.

Linux dependencies

RStudio will automatically detect and install the required R packages, but it can’t install any system-level (Linux) dependencies. These need to be installed separately.

On Ubuntu, you can install those dependencies using the following command:

sudo apt-get install libnetcdf-dev libgdal-dev libproj-dev libgeos-dev libudunits2-dev libsqlite3-dev libcurl4-openssl-dev libcurl4-openssl-dev libssl-dev libxml2-dev libfreetype-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev libpng-dev libtiff5-dev libjpeg-dev

Download tutorial files

The eReefs tutorials for R are located in the ereefs-tutorials GitHub repository. Download the repository’s code by clicking Code > Download ZIP. After unzipping the repository’s Zip archive, you will find each R tutorial placed in its own folder, under ereefs-tutorials-main/tutorials/r.

Open tutorial in RStudio

Open RStudio and create a new project: File > New Project > Existing Directory > Browse > select the tutorial folder. Then open the main tutorial qmd file from the Files tab, in the bottom-right panel of RStudio.

The project will open in the main panel of RStudio. To ensure everything runs smoothly, make sure you’re in Source editing mode, in the top-left corner of the editor. The Visual mode can cause issues with running R code reliably.

You can now run the code chunks within the tutorial by clicking the button in the top-right corner of each chunk, in order.

RStudio controls

Keyboard shortcuts

Chunks can also be run be pressing Ctrl+Shift+Enter, and sections of code (e.g. half a line, a single line, multiple lines) can be run by selecting the code and pressing Ctrl+Enter (replace Ctrl with Cmd on Mac).

Installing R packages

You may encounter the situation where an R package used in the tutorials have not yet been installed on your machine. Installing packages in R is easy, just run the following command from an open R console:

install.packages("<package name>")  # replace <package name> with the name of the R package you wish to install