Reputation: 14425
I am trying to run R scripts via BAT files on Windows Command Prompt.
The scripts require a few R packages such as data.table
, tidyR
, etc.
For operational reasons, all required R packages and dependencies (including data.table
) are installed at C:\Users\username\Documents\R\R-3.5.1\library
. I am not allowed to install RStudio in this environment.
When I try
"C:\Program Files\R\R-3.5.1\bin\x64\Rscript.exe" script.R
, I get an error similar to
Error in library(data.table) : there is no package called 'data.table' Execution halted
How can I set the .libPaths
via Command Prompt to point to the correct location of the packages (i.e. to C:\Users\username\Documents\R\R-3.5.1\library
)?
Thank you in advance.
Upvotes: 4
Views: 3538
Reputation: 916
Disclaimer: I'm unfamiliar with R
.
From R: Search paths :
The library search path is initialized at startup from the environment variable R_LIBS (which should be a colon-separated list of directories at which R library trees are rooted) followed by those in environment variable R_LIBS_USER. Only directories which exist at the time will be included.
By default R_LIBS is unset, and R_LIBS_USER is set to directory ‘R/R.version$platform-library/x.y’ of the home directory (or ‘Library/R/x.y/library’ for CRAN macOS builds), for R x.y.z.
An environment variable can be created with set VARIABLE_NAME=YOUR_VALUE
batch command.
So your batch file should probably be something like this:
cd /d "C:\INSERT_PATH_TO_DIRECTORY_CONTAINING_script.R"
set "R_LIBS=C:\Users\username\Documents\R\R-3.5.1\library"
"C:\Program Files\R\R-3.5.1\bin\x64\Rscript.exe" script.R
However for portability reasons (let's say a collegue asks for a copy of your script or your computer dies) I suggest putting the script, R library and batch file in a single directory, let's say C:\Users\username\Documents\R
. The batch file C:\Users\username\Documents\R\script.bat
becomes:
cd /d "%~dp0"
set "R_LIBS=%~dp0R-3.5.1\library"
"%PROGRAMFILES%\R\R-3.5.1\bin\x64\Rscript.exe" "%~dpn0.R"
%PROGRAMFILES%
environment variable expands to full path of program files
folder, %~dp0
parameter expands to full path of a directory that holds your batch file, and %~dpn0
is a batch-file full path without extension.
Notice that %~dp0R-3.5.1
is not a typo because %~dp0
includes trailing backslash.
This way you can copy C:\Users\username\Documents\R
to D:\Users\SOMEOTHERNAME\Documents\R
and the script will still run.
If you create another version of your script, just copy the batch file so that it has same filename as your script but .bat
extension instead of .R
and it should call the new script - this has proven to be very handy when debugging and distributing scripts.
Alternatively, if you would rather install libraries separately you may want to use %HOMEDRIVE%%HOMEPATH%
which expands to C:\Users\username
.
Extracting proper Documents
folder path, as well as R
installation path is possible but requires reading the registry and thus is a bit more complicated.
Upvotes: 2