15

How do I execute a command using RScript myfile.R so that it uses the renv environment of the project/directory it's in, NOT my default environment?

0

3 Answers 3

9

There are a couple ways:

  1. Ensure your working directory is set to the root of your renv project, and that the renv project's auto-loader is active. (You can set up the auto-loader by calling renv::activate() from R in that project.)

  2. In your script, explicitly call renv::load("/path/to/project") to load the requested project.

If neither of these methods suffice, please file an issue at https://github.com/rstudio/renv/issues.

5

I believe a simpler way than the above answers:

Rscript -e 'renv::run("/path/to/myscript.R")'

It will pick up the renv environment from the base path. You can also specify the environment using the project parameter.

4

I recently had a similar problem but the answer by @kevin-ushey was insufficient. Here's the background. I need to be able to run Rscript from any directory (Because I had several statistical models which were to be called from a Docker file, forcing a Docker file to have WORKDIR many times is just too cumbersome when you have long files with several Rscript calls. Moreover, some of these models are called several times in different bash files, making it cumbersome to cd to the directory before every Rscript call). We needed something akin to conda activate where any Rscript call would just use the activated 'renv environment' by default, regardless of what your working directory is. Here's a dummy example:

  1. Install renv with install.packages('renv').

  2. Create dummy folder with a dummy script containing the beepr library (just for the sake of the example) and initialize the renv environment:

mkdir ~/renv_test/
cd ~/renv_test/
echo "library(beepr); print('success')" >> test.R
Rscript -e "renv::init()"
  1. Create a Docker image with the code below:
FROM rocker/r-base

ENV PROJ_ROOT='/usr/local/src/renv_test'
ENV RENV_DIR='/usr/local/.renv/'

COPY . $PROJ_ROOT

# Copy the projects renv infrastructure to RENV_DIR and remove all traces of renv from PROJ_ROOT
RUN mkdir -p $RENV_DIR/renv/ && \
  cp $PROJ_ROOT/renv.lock $RENV_DIR && \
  cp $PROJ_ROOT/renv/activate.R $RENV_DIR/renv/ && \
  echo "source('renv/activate.R')" >> $RENV_DIR/.Rprofile && \
  cd $RENV_DIR && \
  Rscript -e "renv::restore()" && \
  cd $PROJ_ROOT && Rscript -e "renv::deactivate()" && \
  rm -rf renv/ renv.lock

# Set RENV_DIR's restore library as the default library
RUN echo $(cd $RENV_DIR && Rscript -e "cat(paste0('R_LIBS=', renv::paths\$library()), sep = '\n')") >> $HOME/.Renviron


# Run any script from any directory as if you had 'renv activated'
CMD Rscript $PROJ_ROOT/test.R

Here's a summary of the approach:

  • Copy the project to the docker image
  • Copy the renv infrastructure to a separate folder (here ~/.renv/) and restore the project there.
  • Eliminate all traces of renv from the project folder (this is so we don't mess up the path of the library if for some reason we execute a script from the root of this project).
  • Edit .Renviron so that it contains the restored library path in ~/.renv as the default library. This ensures that any new R session will use that library as the first option.
  • Execute any R scripts located in the project folder without having to cd or WORKDIR (docker) to the project folder.

If you build and run the previous Docker image, you should get a success statement even though we never cd to the project folder:

docker build -t renv_test .
docker run renv_test
[1] "success"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.