Disconnect all packages while running in R

While working on a solution to another problem, I had this problem:

I can delete all R objects with:

rm(list = ls(all = TRUE)) 

Is there an equivalent command that can disable installed packages during a working session?

 > sessionInfo() R version 2.12.2 (2011-02-25) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] stats graphics grDevices utils datasets methods base 

required (ggplot2)

 Loading required package: ggplot2 Loading required package: reshape Loading required package: plyr Attaching package: 'reshape' The following object(s) are masked from 'package:plyr': round_any Loading required package: grid Loading required package: proto 

sessionInfo ()

 R version 2.12.2 (2011-02-25) Platform: i386-pc-mingw32/i386 (32-bit) locale: [1] LC_COLLATE=English_United States.1252 [2] LC_CTYPE=English_United States.1252 [3] LC_MONETARY=English_United States.1252 [4] LC_NUMERIC=C [5] LC_TIME=English_United States.1252 attached base packages: [1] grid stats graphics grDevices utils datasets methods [8] base other attached packages: [1] ggplot2_0.8.9 proto_0.3-9.1 reshape_0.8.4 plyr_1.4 

I tried this way, although even it did not work in a global solution:

 pkg <- c("package:ggplot2_0.8.9", "package:proto_0.3-9.1", "package:reshape_0.8.4", "package:plyr_1.4") detach(pkg, character.only = TRUE) Error in detach(pkg, character.only = TRUE) : invalid 'name' argument In addition: Warning message: In if (is.na(pos)) stop("invalid 'name' argument") : the condition has length > 1 and only the first element will be used 

What I'm looking for is something global:

  rm(list = ls(all = TRUE)) 

for objects, expect it to not remove attached base packages

thank;

+86
r workspace
Sep 21 '11 at 19:31
source share
8 answers

So, someone just had to answer 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, use instead.

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

Note that using invisible (*) is optional, but may be useful to prevent vertical spam in the NULL response message.

+88
Aug 30 '16 at 18:53
source share

Please try the following:

 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() 
+51
Jan 11 '12 at 10:12
source share

You were close. Note that ?detach should say about the first argument to name detach() :

Arguments:

 name: The object to detach. Defaults to 'search()[pos]'. This can be an unquoted name or a character string but _not_ a character vector. If a number is supplied this is taken as 'pos'. 

Therefore, we need to repeatedly call detach() once per item from pkg . There are several other arguments that we need to specify in order to make this work. The first character.only = TRUE , which allows the function to assume that name is a character string - without it, it will not work. Secondly, we also probably want to unload any associated namespace. This can be achieved by setting unload = TRUE . Thus, there is a solution, for example:

 pkg <- c("package:vegan","package:permute") lapply(pkg, detach, character.only = TRUE, unload = TRUE) 

Here is a complete example:

 > require(vegan) Loading required package: vegan Loading required package: permute This is vegan 2.0-0 > sessionInfo() R version 2.13.1 Patched (2011-09-13 r57007) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_GB.utf8 LC_NUMERIC=C [3] LC_TIME=en_GB.utf8 LC_COLLATE=en_GB.utf8 [5] LC_MONETARY=C LC_MESSAGES=en_GB.utf8 [7] LC_PAPER=en_GB.utf8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods [7] base other attached packages: [1] vegan_2.0-0 permute_0.7-0 loaded via a namespace (and not attached): [1] grid_2.13.1 lattice_0.19-33 tools_2.13.1 > pkg <- c("package:vegan","package:permute") > lapply(pkg, detach, character.only = TRUE, unload = TRUE) [[1]] NULL [[2]] NULL > sessionInfo() R version 2.13.1 Patched (2011-09-13 r57007) Platform: x86_64-unknown-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_GB.utf8 LC_NUMERIC=C [3] LC_TIME=en_GB.utf8 LC_COLLATE=en_GB.utf8 [5] LC_MONETARY=C LC_MESSAGES=en_GB.utf8 [7] LC_PAPER=en_GB.utf8 LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_GB.utf8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods [7] base loaded via a namespace (and not attached): [1] grid_2.13.1 lattice_0.19-33 tools_2.13.1 

If you want to turn this into a function, examine the code in sessionInfo() to find out how it identifies what it means as "other attached packages:". Combine this bit of code with the idea above in one function and you are at home and dry. I will leave it to you, though.

+29
Sep 21 '11 at 19:43
source share

nothing

It might be worth adding a solution made by Romain FranΓ§ois . When you download the nothing package, which is currently available on GitHub , it will unload all downloaded 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

Using the devtools package:

 devtools::install_github("romainfrancois/nothing") 

pacman

An alternative approach uses the pacman package available through CRAN:

 pacman::p_unload(pacman::p_loaded(), character.only = TRUE) 
+22
Jan 24 '17 at 12:52 on
source share

Building Gavin's answer, but not a complete function, would be as follows:

 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) 
+9
Sep '11 at 20:20
source share

or if you have RStudio, just uncheck all the boxes in the Packages tab to disconnect

+4
Jun 05 '17 at 7:51 on
source share

In most cases, the problem is with plyr vs dplyr . Use this at the beginning of the code:

 detach("package:plyr", unload=TRUE) 

Therefore, whenever a script is executed, it clears the plyr package

0
Dec 24 '17 at 1:45
source share

If you have problems with packages whose functions with the same name conflict with each other, you can always refer to the namespace of the package whose function you want.

 pkg_name::function_i_want() 
-one
Jan 25 '19 at 14:27
source share



All Articles