Skip to content

Instantly share code, notes, and snippets.

@vjcitn
Created February 16, 2020 13:48
Show Gist options
  • Save vjcitn/d7b82160907a65fb3a4e83a2e1a1a92c to your computer and use it in GitHub Desktop.
Save vjcitn/d7b82160907a65fb3a4e83a2e1a1a92c to your computer and use it in GitHub Desktop.
# probably needs to do something to inform R that a new package is available?
# the code can be used but will installed.packages() work properly?
lib2 = function(pkgname, dry.run=TRUE, repos=BiocManager::repositories(),
character.only=FALSE, source_gs="gs://biocbbs_2020a/packs_3.10/",
target=.libPaths()[1], ...) {
if (!character.only) pkgname = as.character(substitute(pkgname))
stopifnot(is.character(pkgname) && is.atomic(pkgname) && length(pkgname)==1)
# verify need
ino = options(no.readonly=TRUE)
options(repos=repos)
already_in = rownames(installed.packages())
if (pkgname %in% already_in) return(base::library(pkgname, character.only=TRUE, ...))
# make sure you can get into target folder
tst = try(setwd(target)) # now in future .libPaths
if (inherits(tst, "try-error")) stop("can't change folder to target")
# learn dependencies needed
deps = tools::package_dependencies(pkgname)
nd = names(deps)
deps = unique(c(nd, unlist(deps)))
must_get = setdiff(deps, already_in)
if (length(must_get)==0) return(base::library(pkgname))
curd = getwd()
on.exit({options(ino); setwd(curd)})
if (dry.run) return(list(msg=paste0("dry.run = TRUE so not installing from ", source_gs), deps=deps,
must_get=must_get))
for (i in must_get) {
dir.create(i)
setwd(i)
localize(paste0(source_gs, i), ".", dry=FALSE) # could use gsutil_cp
setwd("..")
}
base::library(pkgname, character.only=TRUE)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment