Managing R versions

Issue

I’ve updated to R 4.2, but have projects that were built with 4.0 and even 3.x. Most new versions of packages for R 4.x don’t work in 3.x, and 4.2 seems to have broken quite a bit compared to 4.0. So I can’t just renv::restore(), and would need to update packages one at a time, but I know doing that will break things, and I don’t have time to do a full update of the project.

I use renv to manage the packages, but not currently anything to switch/manage R versions itself. In python, there’s pyenv to manage python versions. I’ve run across rig (https://github.com/r-lib/rig).

Install

click on windows installer. Restart terminal. Type rig list to see what R is available.

Using it

Go to the project I want to run, and figure out what version of R it was using. The one I’m testing on uses 4.0.2, which still is different enough from 4.2 that a lot of the packages fail when I renv::restore() in a session with 4.2. To avoid any issues, I will downgrade to that to run the project because I really just need things to work. Then, once I know it works, I can start updating and testing.

So, try rig add 4.0.2. Seems to have worked. Set the default to current, though.

rig default 4.2.1.

Choosing for a project

What if I just open the project file by double clicking? There’s no obvious way to change the R version just by opening Rstudio- it uses the default.

I think there’s probably a way to use the CLI to change the R version and then double click, but what seems to be easiest is cd path/to/repo and then rig rstudio renv.lock to open with the version in the lockfile.

Note: this does not work with the new version of Rstudio (2022.12.0 Build 353 and others): https://github.com/r-lib/rig/issues/134 and https://github.com/rstudio/rstudio/issues/12545

And do I keep using other R versions elsewhere? Seem to. For now, this should do what I need.

Installing rtools

We need rtools to install packages with compiled components. R 4.2 has updated to Rtools 42 (from 40), and so using previous versions of R need older Rtools. The telltale is when trying to install a package, we get errors about ‘make’ not being found. The rig documents imply that rig system update-rtools40 should work, but I get “Error: the system cannot find the path specified”. I’m not sure what path that is, so hard to fix. So, I seem to be OK until I need something that needs ‘make’, and then I’m out of luck.

The solution

To install Rtools40, needed for R 4.0- 4.1, run rig add rtools40. Seems to be all it took, now I can compile. I assume there’s a similar command for even older Rtools if need to downgrade to R 3.x, but haven’t tried.

Maybe not the solution

As of R4.4, using rig system update-rtools40 seems to work for installs with install.packages, renv, and remotes, but when I install with pak I get pkgbuild errors. And yet, when I try the suggested pkgbuild::check_build_tools() it says ‘Your system is ready to build packages!’. Installing rtools directly from the website (not rig) seems to fix it.