Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"         
like image 655
pbreach Avatar asked Dec 28 '14 01:12

pbreach


People also ask

How do I enable a package in R?

packages(' ') command. Alternatively, you would go to the packages field in Rstudio, click install packages, choose the package and click install. If you have the package on your computer, it is not ready to use yet. You need to attach/load/activate (synonyms) the package.

Where is R package folder?

R packages are a collection of R functions, complied code and sample data. They are stored under a directory called "library" in the R environment. By default, R installs a set of packages during installation.

Do you need to install packages every time in R?

You only need to install packages the first time you use R (or after updating to a new version). **R Tip:** You can just type this into the command line of R to install each package. Once a package is installed, you don't have to install it again while using the version of R!


1 Answers

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) } # ===================================================================== 
like image 112
Jthorpe Avatar answered Sep 26 '22 13:09

Jthorpe