Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rscript and Packages: How and when to determine what packages are loaded?

I want to execute a script file.R using Rscript. In file.R, I use the package dplyr.

# file.R
df <- data.frame(ID,x,y,z,...)
library(dplyr)
filter(df, ID != "null")
......

If I don't specify any options in the batch-file, everything works fine as file.R includes the line library(dplyr)

# 1) no specification of packages in the batch file  
Rscript.exe file.R arg1 arg2 arg3 > outputFile.Rout 2>&1

However, if I add default-packages=utils in the batch file,

# 2) specification of packages utils in the batch file
Rscript.exe  default-packages=utils file.R arg1 arg2 arg3 > outputFile.Rout 2>&1

the part of file.R using dplyrdoesn't work anymore (Error in filter(df, ID != 'null') : Object 'ID' could not be found)

Since ?Rscript says

--default-packages=list
where list is a comma-separated list of package names or NULL

I tried adding --default-packages=utils,dplyr,

# 3) specification of packages utils and dplyr in the batch file
Rscript.exe  default-packages=utils,dplyr file.R arg1 arg2 arg3 > outputFile.Rout 2>&1

which causes the same error as in 2

Why is batch file 1 the only one that works? I am calling the same R script in all 3 alternatives.

like image 294
rmuc8 Avatar asked Apr 14 '15 11:04

rmuc8


People also ask

Do you need to load 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!

How do you get documentation of an installed and loaded R package?

Besides finding the DESCRIPTION files such as cran.r-project.org or stat.ethz.ch, you can also access the description file inside R with the command packageDescription("package") , via the documentation of the package help(package = "package") , or online in the repository of the package.

What is a package how do you include and use packages in R?

Advertisements. 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. More packages are added later, when they are needed for some specific purpose.

How do I load a specific version of an R package?

To install a specific version of a package, we need to install a package called “remotes” and then load it from the library. Afterwards we can use install_version() by specifying the package name and version needed as shown below.

How to check which packages are loaded in R?

How to check which packages are loaded in R? We can do this by using sessionInfo (). So here we have base packages and ggplot2 version 3.2.1 package currently loaded in my R.

What is the shelf() function in R?

Like {pacman}, the shelf () function from the {librarian} package automatically installs, updates, and loads R packages that are not yet installed in a single function. The function accepts packages from CRAN, GitHub, and Bioconductor (only if Bioconductor’s Biobase package is installed).

Should I load extra packages into my R profile?

If you, by default, load extra packages (e.g. using your .RProfile file) I suggest you avoid doing that, as it's a recipe for disaster. Normally you should only have the base packages loaded: stats, graphics, grDevices, utils, datasets, methods, and base.

What are the default packages in R?

Packages in R Programming language are a set of R functions, compiled code, and sample data. These are stored under a directory called “library” within the R environment. By default, R installs a group of packages during installation. Once we start the R console, only the default packages are available by default.


1 Answers

The --default-packages parameter specifies the packages you want to load by default. It doesn't add to the list of default packages - it replaces the list. Which means that you need to specify all the other base packages you are relying on as well. You can see this by making a simple test script that calls sessionInfo()

In file "env.R":

sessionInfo()

Call from the terminal: Rscript env.R

R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  base 

Now I modify that call: Rscript --default-packages=utils env.R

R version 3.1.2 (2014-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252 
[2] LC_CTYPE=English_United States.1252   
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.1252    

attached base packages:
[1] utils base 

So you do need to specify the other missing packages.

RScript --default-packages=stats,graphics,grDevices,utils,datasets,base,methods env.R

and I threw methods in there too.

With that said if you weren't having any issues when you just ran it with RScript I don't understand why you're trying to mess with the default-packages argument. It seems like you're just creating problems for yourself unless there are other issues you are trying to solve that you're not telling us.

like image 91
Dason Avatar answered Nov 15 '22 05:11

Dason