01. Setting up an R environment

guides
computing environments
r
rstudio

In this guide, we will explore options for setting up an R environment. We will discuss local, remote, and virtual environments. Each have their own advantages and shortcomings. The best option for you will depend on your needs and preferences.

Environment setups

Local environments

Choosing to work with R locally means that you will install R and an integrated development environment (IDE) on your local computer. This approach offers the following advantages:

  • Fast and responsive performance
  • No (inherent) reliance on internet connectivity
  • Increased flexibility to customize your environment

The main disadvantages of working locally are:

  • you will need to install R and an IDE on your local computer,
  • manage your own software environment, and
  • manage your own backups and version control for collaborative projects.

Running and managing R locally can sometimes be a challenge for new users, compared to some other methds. However, there are important advantages to this approach. Remember there are a number of resources available to help you get started and troubleshoot any issues you may encounter.

To get started, install R from CRAN. You can download the latest version of R for your operating system here. Once you have installed R, you will need to install an IDE or editor. For complete beginners, I recommend RStudio, a free and open-source IDE for R. RStudio provides a number of features that make it easier to work with R. If you have experience with programming and/ or are looking for a more customizable editor, you may prefer to use Visual Studio Code. Setting up VS Code for R can be found here.

Remote environments

An alternative to running R locally is to work with R in the cloud. This is known as a remote environment. There are a number of cloud-based options for working with R, including Posit Cloud, Google Colab, and Microsoft Azure. These options provide an R environment that you can access from any computer with an internet connection.

Remote environments provide an environment where you can create, edit, and run R projects from anywhere with internet access. They offers several advantages:

  • No need to install R or an IDE/ editor locally
  • Access your projects from any device
  • Collaborate with others in real-time
  • Easily share your work

Some of the drawbacks of working in the cloud include

  • Reliance on stable internet connection
  • Potential latency and performance issues
  • Somewhat limited customization options compared to a local setup

To get started with Posit Cloud, you will need to create an account. You can sign up for a free account here. Once you have created an account, you will see a list of spaces. By default you will have your personal workspace, but you can also join or be invited to other spaces. Instructors may create spaces for their courses which can provide pre-configured environments for students.

Figure 1: Posit Cloud interface

Visit the Guide documentation to learn more about the features of Posit Cloud.

Virtual environments

If you are new to R, you may want to consider working in the cloud to get started. If you plan to continue to work with R in the future, you will most likely want to install R and an IDE/ editor on your local computer or explore using a virtual environment. Virtual environments, such as Docker, provide a way to use a pre-configured computing environment or create your own that you can share with others. Pre-configured virtual environments exist for R through the Rocker project and can be used locally or in the cloud.

In addtion to the advantages of working with R locally, using Docker with Rocker offers several benefits:

  • Safe and isolated environment from the host system
  • Reproducible and portable environments
  • Simplified dependency management using {pak}

The drawbacks to using Docker with Rocker include:

  • Learning curve for setting up and managing Docker containers
  • Increased memory and resource requirements for the host machine
  • Increased complexity in managing Git/ GitHub credentials

To start using Docker with a Rocker image, follow these steps:

  1. Install Docker on your local machine (pay special attention to the installation instructions for your operating system)
  2. Pull the desired Rocker image from Docker Hub
Figure 2: Pull rocker/rstudio image from Docker Hub
  1. Run a container using the pulled image
    Note: you will need to specify the following options before running the container.
  • container name: no spaces
  • port mapping: 8787 on the host to 8787 on the container
  • environment variables: PASSWORD to set the password for RStudio, ROOT to allow root access.
Figure 3: Run a container using the rocker/rstudio image

Optional: you can also mount a volume to share files between the host and container.

  1. Access RStudio in your browser at http://localhost:8787 and log in with username rstudio and the password you set

Summary

In this guide, we have discussed strategies for working with R. All three options offer unique advantages. In Table 1, we summarize some of the characteristics, benefits, and drawbacks of each option.

Table 1: Comparison of different environments for working with R and RStudio
Environment Characteristics Benefits Drawbacks
Local (Computer) - R/RStudio installed locally
- Project files stored on local machine
- Accessible without internet connection
- Full control over software version and environment
- Fast and responsive performance
- No reliance on internet connectivity
- Ability to work offline
- Complete control over software version and environment
- Limited collaboration options
- Difficulty in sharing projects with others
- Potential compatibility issues with different operating systems
Remote (Cloud) - R/RStudio accessed via web browser
- Project files stored on cloud server
- Accessible from any device with internet connection
- Easy collaboration with others
- Automatic backups and version control
- No need for local installation or setup
- Easy access from anywhere
- Seamless collaboration with teammates
- Backup and version control provided by the cloud service
- Reliance on stable internet connection
- Potential latency and performance issues
- Limited customization options compared to a local setup
Virtual (Docker) - R/RStudio environment encapsulated in a Docker container
- Project files stored locally or on the cloud
- Consistent environment across different machines
- Reproducible and portable environment
- Easy setup and sharing of the container
- Flexibility to run on different operating systems
- Isolation from host system dependencies
- Learning curve for setting up and managing Docker containers
- Increased memory and resource requirements
- Potential compatibility issues with certain packages or libraries

Give them a try and see which one works best for your needs! Remember, you can always switch between different environments as your needs change.

References