Renv (pronounced "arr env") is a tool for installing multiple versions of R alongside each other.
A new version of R is released every few months, and with each release comes new changes. These changes can make code and packages break or not work in the same way. For ongoing projects, it's a good idea to update your code to work with the new versions, but sometimes you either don't want to or can't do that.
Renv is a tool that helps freeze your R version to alleviate this problem. This tool works with your terminal shell to switch R versions automatically.
git clone https://github.com/viking/Renv.git ~/.RenvIf you don't want to use git, you can download a zip of Renv instead. Unzip it in your home folder and rename the Renv directory (called
Renv-Renv
) to .Renv
.
.bashrc
file (for Linux) or the .bash_profile
file (for Mac) and add the following to the end:
export PATH="$HOME/.Renv/bin:$PATH" eval "$(Renv init -)"
Or you can run one of these commands to do it for you:
Linux:echo 'export PATH="$HOME/.Renv/bin:$PATH"' >> ~/.bashrc echo 'eval "$(Renv init -)"' >> ~/.bashrcMac:
echo 'export PATH="$HOME/.Renv/bin:$PATH"' >> ~/.bash_profile echo 'eval "$(Renv init -)"' >> ~/.bash_profileYou'll need to restart your terminal after doing this.
mkdir -p ~/.Renv/plugins git clone git://github.com/viking/R-build.git ~/.Renv/plugins/R-buildIf you're not using git, first create the
plugins
folder inside the .Renv
folder. Download the R-build zipfile and unzip it in the .Renv/plugins
folder. Then rename the R-build directory (called R-build-R-build
) to R-build
.
Next, you can install a version of R by running:
Renv install 2.15.2
This will take a while.
After it's done run this command to refresh Renv:Renv rehashYou may need to install some additional packages to build R. If you're on Ubuntu, run this command:
sudo apt-get build-dep r-base
If you want to use Renv in combination with RStudio, you need to build R a little differently, like so:
CONFIGURE_OPTS="--enable-R-shlib" Renv install 2.15.2Notice the additional
CONFIGURE_OPTS
bit. RStudio needs the R shared library in order to work.
By default, Renv uses whatever version of R is installed globally on your system. There are a few ways to use a "frozen" version of R.
Renv global 2.15.2This will set the default version of R. You can undo this by running:
Renv global system
Renv local 2.15.2While you are in the project directory, the R version used will be the one you just specified. This overrides the global setting. You can undo this by running:
Renv local --unset
Renv shell 2.15.2After running this command, whenever you use R during the current terminal session, it will be the version you just specified. This overrides both the global and local settings. You can undo this by running:
Renv shell --unset
install_version
function will let you install a specific version of a CRAN package, even if that package has been pulled from CRAN.
Additionally, you can use the .Rprofile
file to set a local library path by calling the .libPaths
function. After doing this, R will install and load packages from a directory you specify. This way you can freeze package versions in your project directory.
I | Attachment | Action | Size | Date | Who | Comment |
---|---|---|---|---|---|---|
odp | increasing-reproducibility.odp | manage | 492 K | 16 Nov 2012 - 13:21 | JeremyStephens | Open document presentation |
increasing-reproducibility.pdf | manage | 430 K | 16 Nov 2012 - 13:21 | JeremyStephens | PDF presentation |