R/hide-site-libraries.R

replaceLibrary <- function(lib, value) {

  ## Need to clobber in package:base, namespace:base
  envs <- c(
    as.environment("package:base"),
    .BaseNamespaceEnv
  )

  for (env in envs) {
    do.call("unlockBinding", list(lib, env))
    assign(lib, value, envir = env)
    do.call("lockBinding", list(lib, env))
  }

}


hideLibrary <- function(lib) {
  replaceLibrary(lib, character())
}

restoreLibrary <- function(lib) {

  cachedLib <- if (lib == ".Library")
    getenv("R_PACKRAT_SYSTEM_LIBRARY")
  else if (lib == ".Library.site")
    getenv("R_PACKRAT_SITE_LIBRARY")

  if (is.null(cachedLib)) {
    warning("packrat did not properly save the library state; cannot restore")
    return(invisible(NULL))
  }

  replaceLibrary(lib, cachedLib)

}

## Remove the site-library libraries from unix-alikes
hideSiteLibraries <- function() {
  hideLibrary(".Library.site")
}

## Restore the site-library libraries
restoreSiteLibraries <- function() {
  restoreLibrary(".Library.site")
}

Try the packrat package in your browser

Any scripts or data that you put into this service are public.

packrat documentation built on May 2, 2019, 6:24 a.m.