R/fn_unload.R

#' Unload packages
#' @description unload_packages() is an Unload function that performs a custom detaching of a package from the search path. Specifically, this function implements an algorithm to unload packages. The function is called for its side effects and does not return a value.
#' @param package_chr Package (a character vector)
#' @return No return value, called for side effects.
#' @rdname unload_packages
#' @export 
#' @importFrom purrr discard map_lgl walk
#' @keywords internal
unload_packages <- function (package_chr) 
{
    if (!package_chr %>% purrr::discard(is.na) %>% identical(character(0))) {
        loaded_pck_chr <- search()
        unload_chr <- package_chr[purrr::map_lgl(package_chr, 
            ~paste0("package:", .x) %in% loaded_pck_chr)]
        if (!unload_chr %>% purrr::discard(is.na) %>% identical(character(0))) {
            purrr::walk(unload_chr, ~unloadNamespace(.x))
        }
    }
}
ready4-dev/ready4fun documentation built on April 22, 2024, 8:33 a.m.