R/hide-site-libraries.R

Defines functions restoreSiteLibraries hideSiteLibraries restoreLibrary hideLibrary replaceLibrary

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 Sept. 8, 2023, 5:44 p.m.