detach all packages while working in R

后端 未结 10 1834
猫巷女王i
猫巷女王i 2020-11-28 19:23

While working to solve another problem I got this problem:

I can remove all R objects by:

rm(list = ls(all = TRUE))

Is there equiv

10条回答
  •  猫巷女王i
    2020-11-28 20:04

    Building on Gavin's answer but not quite to a full function would be this sequence:

    sess.pkgs <- function (package = NULL) 
    {   z <- list()
           if (is.null(package)) {
            package <- grep("^package:", search(), value = TRUE)
            keep <- sapply(package, function(x) x == "package:base" || 
                !is.null(attr(as.environment(x), "path")))
            package <- sub("^package:", "", package[keep])
        }
        pkgDesc <- lapply(package, packageDescription)
        if (length(package) == 0) 
            stop("no valid packages were specified")
        basePkgs <- sapply(pkgDesc, function(x) !is.null(x$Priority) && 
            x$Priority == "base")
        z$basePkgs <- package[basePkgs]
        if (any(!basePkgs)) {
            z$otherPkgs <-  package[!basePkgs]
        }
        z
    }
    
    lapply(paste("package:",sess.pkgs()$otherPkgs, sep=""), detach, 
                                 character.only = TRUE, unload = TRUE)
    

提交回复
热议问题