35

I want to run R files in batch mode using Rscript, however it does not seem to be loading the libraries that I need. The specific error I am getting is:

Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted

However I do have the package timeSeries and can load it from Rstudio, RGui, and R from the command line no problem. The issue seems to only be when running a script using Rscript.

My system/environment variables are configured as:

C:\Program Files\R\R-3.1.0\bin\x64 (Appended to PATH)
R_HOME = C:\Program Files\R\R-3.1.0
R_User = Patrick

I am running the same version of R in RStudio, RGui, and R from command line. I've also checked .Library from these three sources and got the same output as well.

How can I run Rscript from command line with the packages that I am using (and have installed) in R?

EDIT:

I am using Rscript via Rscript script.r at the windows command line in the directory where script.r is located.

The output of Rscript -e print(.Library) is [1] "C:/PROGRA~1/R/R-31~1.0/library"

which is consistent with the other three options that I mentioned: [1] "C:/PROGRA~1/R/R-31~1.0/library"

However, if I put this in my script:

print(.libPaths()) 
library(timeSeries) #This is the package that failed to load

I get an output of:

[1] "C:/Program Files/R/R-3.1.0/library"
Error in library(timeSeries) : there is no package called 'timeSeries'
Execution halted

The corresponding call in RStudio gives an additional path to where the package is actually installed:

> print(.libPaths())
[1] "C:/Users/Patrick/Documents/R/win-library/3.1" "C:/Program Files/R/R-3.1.0/library"    

  
Community
  • 1
  • 1
pbreach
  • 16,049
  • 27
  • 82
  • 120
  • @flodel -As requested – pbreach Dec 28 '14 at 01:55
  • 2
    ok... Now start over with `.libPaths()`... You could also add `print(.libPaths())` right before `library(timeSeries)` inside `script.r`. – flodel Dec 28 '14 at 02:02
  • Ok great! So now we know the issue. Rscript library path is not connected to where the package is actually installed (see edit) but RStudio and the other are. – pbreach Dec 28 '14 at 02:32
  • 4
    So your quick fix is to add `.libPaths(c("C:/Users/Patrick/Documents/R/win-library/3.1", .libPaths()))` somewhere before calling `library(timeSeries)`. I am not sure why the `.libPaths()` differ in the first place. It would be nice if you could play with the info at `?.libPaths` to find the reason, along with a more permanent solution. – flodel Dec 28 '14 at 02:54
  • Yup it works! Would you like to post that as an answer? According to the docs the permanent solution would be to set the paths in the environment variable `R_LIBS`. I'm not sure why the paths differ with RScript and not the other. Maybe this is an effort to reduce startup time as RScript normally does not load as many things on startup in comparison. – pbreach Dec 28 '14 at 02:58
  • About posting as an answer... I'd really like to get to the bottom of it before that. Could you tell under what name "C:/Users/Patrick/Documents/R/win-library/3.1" shows up when running `Sys.getenv(c("R_LIBS", "R_LIBS_USER", "R_LIBS_SITE"))` in RStudio? – flodel Dec 28 '14 at 03:05
  • Sure, no problem. The path mentioned actually turns up under `"R_LIBS_USER"`. In this path is where all of my third party packages are installed and I think was created when I first installed them through RStudio. – pbreach Dec 28 '14 at 03:16
  • Just to add, i already had `.libPaths("path")` on top of the script but it still gave the error. Then i converted it to a vector with the extra argument as @flodel gave in the above comment. like `.libPaths(c("path"), .libPaths()))` Then it worked. May be it helps someone. – sjd Sep 25 '19 at 06:53

5 Answers5

10

In short, the value returned by calling Sys.getenv('R_LIBS_USER') in R.exe needs to be the same as the value returned by calling this at the command line:

Rscript.exe -e "Sys.getenv('R_LIBS_USER')"

and the above value needs to be included in this command line call:

Rscript.exe -e ".libPaths()"

Note that the values of R_LIBS_USER may be differ between R.exe and Rscript.exe if the value of R_USER is changed, either in the .Rprofile or the in target field of user's shortcut to R.exe, and in general, I find that the user library (i.e. .libPaths()[2]) is simply not set in Rscript.exe

Since I'm fond of setting R_USER to my USERPROFILE, I include the following block in at the top of .R files that I wish to run on mulitiple computers or in Rscript.exe's .Rprofile (i.e. Rscript -e "path.expand('~/.Rprofile')"):

# =====================================================================
# For compatibility with Rscript.exe: 
# =====================================================================
if(length(.libPaths()) == 1){
    # We're in Rscript.exe
    possible_lib_paths <- file.path(Sys.getenv(c('USERPROFILE','R_USER')),
                                    "R","win-library",
                                    paste(R.version$major,
                                             substr(R.version$minor,1,1),
                                             sep='.'))
    indx <- which(file.exists(possible_lib_paths))
    if(length(indx)){
       .libPaths(possible_lib_paths[indx[1]])
    }
    # CLEAN UP
    rm(indx,possible_lib_paths)
}
# =====================================================================
Community
  • 1
  • 1
Jthorpe
  • 9,756
  • 2
  • 49
  • 64
7

As mentioned in the comments, it seems Rscript doesn't recognize the library path defaults automatically. I am writing an R script that needs to be source-able from the command line on different people's computers, so I came up with this more general workaround:

  • First store the default library path in a variable (Rscript-sourced functions can find this, they just don't automatiocally)
  • Then include that path in the library() call with lib.loc = argument.
  • This should work regardless of what the path is on a given computer.

    library.path <- .libPaths()
    library("timeseries", lib.loc = library.path)
    

Thanks again to @flodel above for putting me on the right path

rrr
  • 1,914
  • 2
  • 21
  • 24
  • There is one edge case to keep in mind where the above will not work: http://stackoverflow.com/questions/28312901/r-keeps-downloading-packages-to-tmp-directory – Paul Dec 29 '16 at 17:22
4

This answer will not help the original asker (pbreach), but it may help someone else who stumbles across this question and has a similar problem to me.

I have many bash .sh script files which call RScript to execute .R files. My operating system is Windows 10, and I execute those bash files using cygwin.

Everything had been working fine until yesterday, when I finally upgraded my R from Revolution R 8.0.1 beta to Microsoft R Open 3.4.1. After that upgrade, every bash script that called RScript failed due to the exact same reason asked here (e.g. Error in library(zoo) : there is no package called 'zoo').

Investigation revealed that RScript actually worked fine if called from a DOS shell instead of from a cygwin bash shell.

For example, if I execute this in a DOS shell

C:\Progra~1\Microsoft\ROpen~1\R-3.4.1\bin\x64\Rscript.exe -e ".libPaths()"

I see the output

[1] "C:/Users/HaroldFinch/Documents/R/win-library/3.4"
[2] "C:/Program Files/Microsoft/R Open/R-3.4.1/library"

I eventually discovered the reason. As explained in the R FAQ, to define its home directory, R will first use the R_USER environment variable if defined, else it will use HOME environment variable if defined, else it will use the Windows "personal" directory.

My Windows configuration does not define either R_USER or HOME environment variables. So, in the DOS shell case, R uses my Windows "personal" directory (C:/Users/HaroldFinch/Documents). That is good, because that is where all my libraries are installed (C:/Users/HaroldFinch/Documents/R/win-library/3.4).

In contrast, cygwin defines and exports a HOME environment variable that points to my cygwin user directory, which lacks any R stuff. Hence, RScript called from cygwin had a wrong R home directory, and so failed to load libraries.

There are probably many ways to solve this. I decided to have my bash script set a R_USER environment variable which points to my Windows user directory.

For example, if I execute this in a cygwin bash shell:

R_USER="C:/Users/HaroldFinch/Documents"
export R_USER
/cygdrive/c/Progra~1/Microsoft/ROpen~1/R-3.4.1/bin/x64/Rscript.exe -e ".libPaths()"

I see the output

[1] "C:/Users/HaroldFinch/Documents/R/win-library/3.4"
[2] "C:/Program Files/Microsoft/R Open/R-3.4.1/library"

which is exactly the same output now as the DOS shell example above.

HaroldFinch
  • 762
  • 1
  • 6
  • 17
0

Another cause is packrat. If you are running with packrat, RStudio turns it on for you when you open the project. RScript does not, so you need a packrat::on() early in your script (before the library calls).

Dov Rosenberg
  • 673
  • 6
  • 20
0

As the others have already pointed out, the problem is that Rscript.exe cannot recognise the win-library folder. The easiest solution for me was to explicitly set the path to the library folder by adding:

.libPaths("C:/Users/Benutzer1/Documents/R/win-library/4.0")

to my program. Then it loads all the packages from the win-library folder and it is still capable of loading packages from the standard library folder.

Jens
  • 2,363
  • 3
  • 28
  • 44