# RStudio and R

# R

# How to create R environment

Here's an example on how to create a Conda environment with R and R-IRkernel kernel and commonly used packages:

mamba create -n <name> 'r-base>=4.0,<5.0' 'r-irkernel' 'r-devtools' 'r-remotes' 'r-dplyr' 'r-tidyverse' 'r-haven' 'r-languageserver' 'r-lintr' 'cmake' 'compilers' 'cmake' 'gmp'

# Rstudio

RStudio is configured to use packages installed in the conda environment named r-base.

# R packages

You can identify your current R version packages in your Workbench Terminal following these steps:

  1. Open your Workbench Terminal.

workbench_terminal.png

  1. List the packages in your r-base conda environment.
conda list -n r-base

# How to install R packages

If you want to install your own R packages, we recommend that you use Conda where ever possible:

  1. Open Workbench Terminal:

workbench_terminal.png

  1. Activate r-base environment
conda activate r-base
  1. Add packages to your r-base code environment:
# -- Principal example
conda install -n r-base -c conda-forge r-'<package-name>' r-'<another-package-name>'

# -- Practical example*
mamba install -n r-base -c conda-forge r-dplyr

For example, the above example installs the dplyr package (opens new window).

  1. When the installation is complete, open RStudio or R in your workbench and load your new package:
# -- Principal example
library(<package-name>)

# -- Practical example
library(dplyr)

You can defer to alternative methods such as install.packages() when the package is not available in Conda (see below).

# How do I install R packages not found in Conda?

If you don't find a R package in Conda as described above, you may need to defer to running the install.packages() command inside R. We recommend to use R in your workbench terminal for such installations (instead of installing directly in RStudio) to avoid broken environment states in RStudio. Packages installed into your r-base environment through R in terminal should be available in RStudio.

Packages installed into r-base environment through R in terminal will be available in RStudio.

Make sure to activate the r-base environment before running R command.

conda activate r-base

# How can I use R in my Workbench Terminal?

First, open your Workbench Terminal.

Start by activating r-base environment:

conda activate r-base

Then run R inside your r-base environment:

R

Enjoy!

# How can I recover my R environment in case of broken package installation?

Sometimes you may get errors about the GLIBCXX library not being found and/or essential R packages are not working. This happens typically after installation of custom package. In such cases, you might consider to recreate your r-base environment from scratch:

  1. Open your workbench terminal and run the following code to reset your r-base environment:
mamba create --yes -n r-base 'r-base>=4.0,<5.0' 'compilers' 'cmake' 'gmp' 'r-irkernel' 'r-devtools' 'r-remotes' 'r-dplyr' 'r-tidyverse' 'r-haven' 'r-languageserver' 'r-lintr'

If you have older setup without mamba, use conda command instead:

conda create --yes -n r-base 'r-base>=4.0,<5.0' 'compilers' 'cmake' 'gmp' 'r-irkernel' 'r-devtools' 'r-remotes' 'r-dplyr' 'r-tidyverse' 'r-haven' 'r-languageserver' 'r-lintr'

WARNING

This removes custom packages that you might have installed.

Last Updated: 11/20/2024