detach all packages while working in R

后端 未结 10 1787
猫巷女王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条回答
  • 2020-11-28 20:00

    So, someone should have simply answered the following.

    lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE)
    

    (edit: 6-28-19) In the latest version of R 3.6.0 please use instead.

    invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE))
    

    Note the use of invisible(*) is not necessary but can be useful to prevent the NULL reply from vertically spamming the R window.

    (edit: 9/20/2019) In version 3.6.1

    It may be helpful to convert loaded only names(sessionInfo()$loadedOnly) to explicitly attached packages first, and then detach the packages, as so.

    lapply(names(sessionInfo()$loadedOnly), require, character.only = TRUE)
    invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE, force=TRUE))
    

    One can attempt to unload base packages via $basePkgs and also attempt using unloadNamespace(loadedNamespaces()). However these typically are fraught with errors and could break basic functionality such as causing sessionInfo() to return only errors. This typically occurs because of a lack of reversibility in the original package's design. Currently timeDate can break irreversibly, for example.

    (edit: 9/24/20) for version 4.0.2 The following first loads packages to test and then gives a sequence to fully detach all packages except for package "base" and "utils". It is highly recommended that one does not detach those packages.

        invisible(suppressMessages(suppressWarnings(lapply(c("gsl","fBasics","stringr","stringi","Rmpfr"), require, character.only = TRUE))))
        invisible(suppressMessages(suppressWarnings(lapply(names(sessionInfo()$loadedOnly), require, character.only = TRUE))))
        sessionInfo()
    
        #the above is a test
    
        invisible(lapply(paste0('package:', c("stringr","fBasics")), detach, character.only=TRUE,unload=TRUE))
        #In the line above, I have inserted by hand what I know the package dependencies to be. A user must know this a priori or have their own automated
        #method to discover it. Without removing dependencies first, the user will have to cycle through loading namespaces and then detaching otherPkgs a
        #second time through.
        invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE,unload=TRUE))
    
        bspkgs.nb<-sessionInfo()$basePkgs[sessionInfo()$basePkgs!="base"]
        bspkgs.nbu<-bspkgs.nb[bspkgs.nb!="utils"]
        names(bspkgs.nbu)<-bspkgs.nbu
        suppressMessages(invisible(lapply(paste0('package:', names(bspkgs.nbu)), detach, character.only=TRUE,unload=TRUE)))
    
        #again this thoroughly removes all packages and loaded namespaces except for base packages "base" and "utils" (which is highly not recommended).
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-11-28 20:04
    #Detach all  packages
    detachAllPackages <- function() {
    
      basic.packages <- c("package:stats","package:graphics","package:grDevices","package:utils","package:datasets","package:methods","package:base")
    
      package.list <- search()[ifelse(unlist(gregexpr("package:",search()))==1,TRUE,FALSE)]
    
      package.list <- setdiff(package.list,basic.packages)
    
      if (length(package.list)>0)  for (package in package.list) detach(package, character.only=TRUE)
    
    }
    
    detachAllPackages()
    

    this will make sure all packages gets detached apart from your basic packages

    0 讨论(0)
  • 2020-11-28 20:06

    or if you have RStudio, simply uncheck all the checked boxes in Packages Tab to detach

    0 讨论(0)
  • 2020-11-28 20:11

    Please try this:

    detachAllPackages <- function() {
    
      basic.packages <- c("package:stats","package:graphics","package:grDevices","package:utils","package:datasets","package:methods","package:base")
    
      package.list <- search()[ifelse(unlist(gregexpr("package:",search()))==1,TRUE,FALSE)]
    
      package.list <- setdiff(package.list,basic.packages)
    
      if (length(package.list)>0)  for (package in package.list) detach(package, character.only=TRUE)
    
    }
    
    detachAllPackages()
    
    0 讨论(0)
  • 2020-11-28 20:11

    nothing

    It may be worth to add solution made available by Romain François. When loaded the package nothing, which is currently available on GitHub, will unload all of the loaded packages; as in the example that Romain provides:

    loadedNamespaces()
    [1] "base"      "datasets"  "grDevices" "graphics"  "methods"   "stats"
    [7] "utils"
    
    require(nothing, quietly = TRUE)
    
    loadedNamespaces()
    [1] "base"
    

    Installation

    With use of the devtools package:

    devtools::install_github("romainfrancois/nothing")
    

    pacman

    An alternative approach uses pacman package available through CRAN:

    pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
    
    0 讨论(0)
提交回复
热议问题