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?
3 Answers
There are a couple ways:
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 callingrenv::activate()
from R in that project.)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.
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.
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:
Install
renv
withinstall.packages('renv')
.Create dummy folder with a dummy script containing the
beepr
library (just for the sake of the example) and initialize therenv
environment:
mkdir ~/renv_test/
cd ~/renv_test/
echo "library(beepr); print('success')" >> test.R
Rscript -e "renv::init()"
- 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
orWORKDIR
(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"