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

In this section, we cover:

R best practices

Default R interpreter

Your Workbench Server Administrator will choose a default R version. The specifics for changing the default will vary by each IDE; refer to the appropriate documentation for more detailed information:

In the terminal

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

which R
# /usr/local/bin/R

Depending on how you installed R, the result of which R may point directly to the R interpreter, or it may point to a symlink. Check by taking the output of which R 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 shows 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. To identify your current active version of R, run:

R.version

Another way to check 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, you could 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

In RStudio Pro

Using the RStudio Pro IDE includes several features that help you easily manage and switch between different versions of R. See the Multiple R Versions page for more details.

Using multiple versions of R

Posit Workbench allows users to switch between multiple versions of R installed on the server. 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. Access to different versions of R can be useful for:

  • 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).

The specifics for changing these settings will vary by each IDE; refer to the appropriate documentation for more detailed information. After changing R versions, the session will need to be restarted to load the changed R version and any dependencies correctly.

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 Admin Guide page, 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 R Versions of the Posit Workbench Admin Guide.

In RStudio Pro

Using the RStudio Pro IDE includes several features that help you easily manage and switch between different versions of R. See the Multiple R Versions page for more details.

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 lets 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 will automatically be activated. To learn more about using renv, visit the package documentation website.

Note

renv supports integration with R and also 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. Check out the resources below for 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 modelling.
  • 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). This will often help as creating an example will help you think about your problem in a way that may help solve it and help capture the problem in a way that will allow others to understand the issue clearly. See here for more on creating a reprex.

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