Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Install a package from, e.g., GitHub into a temporary library
## 2015-11-11 NOTE!
## with_libpaths() is one of the functions removed (well, deprecated, for now) from
## devtools
## it's in withr now!
## see newer script below!!!
library(devtools) # with_lib(), install_github()
tmp_lib <- "~/tmp/tmp_lib"
dir.create(tmp_lib)
## upgrade or downgrade a package here
## following example is upgrading to bleeding edge devel version
with_lib(tmp_lib, install_github("codegenius/alphapkg", ref = "bleeding-edge"))
## but this could also be a downgrade to the CRAN/released version!
## restart R
## explicitly load the affected packages from the temporary library
tmp_lib <- "~/tmp/tmp_lib"
library(alphapkg, lib.loc = tmp_lib)
## your experimentation goes here
## done? clean up!
unlink(tmp_lib, recursive = TRUE)
library(withr)
library(devtools)
tmp_lib <- "~/tmp/tmp_library"
dir.create(tmp_lib)
## upgrade or downgrade a package here
with_libpaths(tmp_lib,
install_github("dill/beyonce"),
"prefix"
)
## restart R
## explicitly load the affected packages from the temporary library
library(beyonce, lib.loc = tmp_lib)
## your experimentation goes here
## done? clean up!
unlink(tmp_lib, recursive = TRUE)
@jennybc
Copy link
Author

jennybc commented May 4, 2015

@jeroenooms points out you could also set up the temporary library like so tmp_lib <- file.path(tempdir(), ".lib") if this will be a very short-lived experiment and you won't even restart R. Advantage: the temporary library will get nuked automatically. If you do this, make sure to begin the whole process in a fresh R session, i.e. there's no risk the affected packages are already loaded.

@stephenturner
Copy link

stephenturner commented May 4, 2015

works with bioconductor also?

@gmbecker
Copy link

gmbecker commented May 4, 2015

@jennybc My switchr package provides an abstraction for doing exactly this, including (trying hard to) unload already loaded versions of packages and protecting you from accidentally mixing.

See http://arxiv.org/abs/1501.02284 and/or the docs/vignette at http://github.com/gmbecker/switchr

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment