R in Posit Workbench

Users can write and execute R code in all four IDEs available in Posit Workbench:

  1. RStudio Pro
  2. VS Code
  3. JupyterLab
  4. Jupyter Notebook

This section covers:

R best practices

Default R interpreter

Your Server Administrator1 defines the default version of R. The method to change the default version varies by each IDE; refer to the applicable documentation linked in the following section for detailed information.

Changing the default R version in the IDE

The guide for each IDE provides detailed information for changing the default version of R or using multiple versions of R::

In the terminal

To identify the path for your default version of R on Workbench, run the following from the terminal:

which R
# /usr/local/bin/R

Depending on how R is configured, the result of which R may point directly to the R interpreter or to a symlink. Verify using the output of which R (from the previous example) and running the following command in the terminal:

ls -l /usr/local/bin/R
# lrwxrwxrwx 1 root root 18 Jul  8 19:21 /usr/local/bin/R -> /opt/R/4.2.0/bin/R

The example above indicates that usr/local/bin/R is a symlink to opt/R/4.2.0/bin/R.


You can also check the R version from within R. Run the following command to identify your current active version of R:

R.version

An alternative way to verify the current version of R, as well as other system and environment information, is to run:

sessionInfo()

You can modify your R’s default version by modifying your PATH. For example, add the following to your ~/.bashrc file:

~/.bashrc
export PATH="/opt/R/4.1.2/bin:$PATH"
which R
# /opt/R/4.1.2/bin/R

Using multiple versions of R

Users can switch between multiple versions of R installed on the server in Workbench. Flexible control over R versions makes it easier to provide newer versions of R for users (or individual projects) that require them; while at the same time not disrupting work that requires the continued use of older versions. Having access to different versions of R helps with:

  • Managing upgrades of R.
  • Testing code on a variety of R versions and distributions.
  • Supporting projects that depend on specific versions of R.

As a user, you can change the version of R:

  • Temporarily, for the duration of the current session (session-level).
  • Every time a specific project is opened (project-level).
  • For every new session opened in the future (user-level).

After changing R versions, restart the session to load the new R version and any dependencies correctly.

The specifics for changing these settings vary by IDE. Refer to the appropriate documentation for detailed information:

In the terminal

A user can call a specific R version to switch R versions or run a script directly in the terminal.

Assuming that your Server Administrator installed R following the instructions from the Installing R documentation, users can identify all available versions of R by running the following command:

$ ls -ld /opt/R/*
/opt/R/4.1.0/
/opt/R/4.2.0/

It is possible to switch between R versions by invoking their absolute paths. For example:

# Start the R REPL user interface using R 4.1
$ /opt/R/4.1.0/bin/R
# Run an existing R script using R 4.2 in the terminal
$ /opt/R/4.2.0/bin/Rscript
Note

Not seeing the R version you need? Your Server Administrator can install R versions for you. Refer them to the R Versions section of the Posit Workbench Administrator Guide.

renv

We recommend using the renv package for projects needing package and environment management.renv can be useful when you have projects with different dependency requirements. For example:

  • It lets you experiment with new packages without breaking other projects. You can test package upgrades before committing to upgrading.
  • You can share the package versions you’re using with your team or future you or enforce a specific version when deploying. This allows you match project environments across different computers.
  • Reuse packages you’ve already installed and know work together.
  • Easily revert to a point in time to aid in troubleshooting if needed.

For a discussion on environment management strategies and how renv can be used with Posit Package Manager to improve your workflows. See Reproducible Environments.

To initialize renv with a new or existing project, run:

renv::init()

Now, whenever you open the project, renv automatically activates. To learn more about using renv, visit the renv package documentation website.

Note

renv supports integration with R and Python (pipenv and reticulate) for multilingual projects.

Additional R resources

Many good resources are available on the web for learning R and seeking answers to questions about how to accomplish various tasks. The resources below provide more information on learning and using R.

Learning R

If you are learning R, there are several good places to start:

  • Our online learning page links to many courses and resources for learning R.
  • R for Data Science is a free book that teaches how to do data science with R, from getting your data into R to transforming, visualizing, and modeling.
  • The Posit cheatsheets give a good overview of using the IDE and popular R packages.
  • Our webinars contain a lot of useful information. Check out the Posit Essentials section for some helpful R material.
  • The R Style Guide provides some guidelines for writing readable and maintainable R code.

Asking questions

If you have a question about a particular R code issue, we’d recommend starting by creating a reproducible example (or reprex).

Creating an example often helps you think through your problem in a solvable way and helps others understand the issue clearly. For more on creating a reproducible example (reprex), see the Tidyverse Get help! page.

You may also want to check out the following resources:

  • The Posit Community is our discussion board for asking questions about R, Shiny, package development, and more.
  • Stack Overflow is an important resource for seeking answers to questions about R - in particular, we’d recommend making sure your question is tagged as “r”.
  • The #rstats Twitter community is very helpful and active.

News and information

The R community is growing rapidly, and many new things are happening all the time. If you want to stay on top of what’s happening, we recommend keeping up with the following sites:

  • Follow the Posit blog to hear about our latest features, packages, and workshops.
  • Follow the Posit RViews blog for general interest articles about R and the R Community.
  • R-bloggers is a blog aggregator that reposts R-related articles from across the web. R-bloggers is a good place to find R tutorials, announcements, and other random happenings.

Other resources

  • RSeek meta search engine: The RSeek meta search engine provides a unified interface for searching the various sources of online R information. If an answer to your question is available online, RSeek can help you locate it.
  • R-help mailing list: The R-help mailing list is a very active list with questions and answers about problems and solutions using R. Before posting to the list, it is recommended to search the list archives to see if an answer already exists.
  • CrossValidated Q&A community: For more statistics-related questions, the CrossValidated Q&A community is a great resource with many R users active on the site.
Back to top

Footnotes

  1. A Server Administrator is responsible for installing and maintaining the Workbench server(s). Typically, this person has SSH access to the server and root privileges. Often, the person in this role works in IT and is not a Workbench user.↩︎