rpackagerscript

Rscript: There is no package called ...?


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"    

  

Solution

  • 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)
    }
    # =====================================================================