Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Install an R package temporarily, only for the current session

Tags:

Sometimes on Stack Overflow, there's a question relative to a package which is not installed on my system, and which I don't plan to reuse later.

If I install the package with install.packages(), it will be put in one of my R install libraries, and then will take some storage space and be updated each time I run update.packages().

Is there a way to install a package only for the current R session ?

like image 231
juba Avatar asked Feb 15 '13 14:02

juba


People also ask

Do you have to install packages in R every time?

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 I stop a package from installing in R?

packages()[, "Package"]) { stop("Package Deriv not installed successfully.") }

What happens if you install a package twice in R?

It shouldn't be an issue unless you install a package as an admin user, and again as a normal user. Then you will have a version in two different locations on your system. That could lead to issues when upgrading, or confusion as to which version is loaded.


1 Answers

You can install a package temporarily with the following function :

tmp.install.packages <- function(pack, dependencies=TRUE, ...) {   path <- tempdir()   ## Add 'path' to .libPaths, and be sure that it is not   ## at the first position, otherwise any other package during   ## this session would be installed into 'path'   firstpath <- .libPaths()[1]   .libPaths(c(firstpath, path))   install.packages(pack, dependencies=dependencies, lib=path, ...) } 

Which you can use simply this way :

tmp.install.packages("pkgname") 

The package is installed in a temporary directory, and its files should be deleted at next system restart (at least on linux systems).

like image 142
juba Avatar answered Sep 19 '22 12:09

juba