While working to solve another problem I got this problem:
在努力解决另一个问题时,我遇到了这个问题:
I can remove all R objects by:
我可以通过:
rm(list = ls(all = TRUE))
Is there equivalent command that can detach installed packages during 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
require(ggplot2)
要求(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()
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 worked in not 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 am loking for is something global like:
我想要的是一种全球性的东西:
rm(list = ls(all = TRUE))
for objects, expect it would not remove attached base packages
对于对象,期望它不会删除附加的基本包
thanks;
谢谢,
7 个解决方案
#1
45
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()
#2
37
So, someone should have simply answered the following.
因此,应该有人简单地回答以下问题。
lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE)
#3
30
You were close. Note what ?detach
has to say about the first argument name
of detach()
:
你是亲密的。注意什么?detach必须说出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’.
So we need to repeatedly call detach()
once per element of pkg
. There are a couple of other arguments we need to specify to get this to work. The first is character.only = TRUE
, which allows the function to assume that name
is a character string - it won't work without it. Second, we also probably want to unload any associated namespace. This can be achieved by setting unload = TRUE
. So the solution is, for example:
因此,我们需要多次调用detach()每一个pkg元素。我们需要指定一些其他参数来让它工作。首先是性格。仅= TRUE,它允许函数假定名称是字符串——没有它就不能工作。其次,我们可能还想卸载任何关联的名称空间。这可以通过设置unload = TRUE来实现。所以答案是,例如:
pkg <- c("package:vegan","package:permute")
lapply(pkg, detach, character.only = TRUE, unload = TRUE)
Here is a full 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, study the code in sessionInfo()
to see how it identifies what it labels as "other attached packages:". Combine that bit of code with the idea above in a single function and you are home and dry. I'll leave that bit up to you though.
如果您想将其转换为函数,请研究sessionInfo()中的代码,看看它是如何将哪些内容标记为“其他附加包:”的。将这段代码与上面的想法结合到一个函数中,您就可以大干一场了。我把这一点留给你来决定。
#4
17
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:
或许有必要增加罗曼·弗朗索瓦(Romain Francois)提供的解决方案。当加载包时,GitHub上当前可用的nothing将卸载所有加载的包;正如罗曼提供的例子:
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包:
devtools::install_github("romainfrancois/nothing")
pacman
An alternative approach uses pacman
package available through CRAN:
另一种方法是使用可通过CRAN获得的pacman包:
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
#5
8
Building on Gavin's answer but not quite to a full function would be this sequence:
以Gavin的答案为基础但不完全是一个完整的函数会是这样的序列:
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)
#6
3
or if you have RStudio, simply uncheck all the checked boxes in Packages Tab to detach
或者,如果您有RStudio,只需取消对Packages选项卡中要分离的所有复选框的选中
#7
1
Most of the times its the plyr
vs dplyr
issue. Use this in the beginning of the code:
大多数时候是plyr和dplyr的问题。在代码的开头使用这个:
detach("package:plyr", unload=TRUE)
So whenever the script runs, its clears the plyr
package
所以无论脚本何时运行,它都会清除plyr包
#1
45
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()
#2
37
So, someone should have simply answered the following.
因此,应该有人简单地回答以下问题。
lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE)
#3
30
You were close. Note what ?detach
has to say about the first argument name
of detach()
:
你是亲密的。注意什么?detach必须说出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’.
So we need to repeatedly call detach()
once per element of pkg
. There are a couple of other arguments we need to specify to get this to work. The first is character.only = TRUE
, which allows the function to assume that name
is a character string - it won't work without it. Second, we also probably want to unload any associated namespace. This can be achieved by setting unload = TRUE
. So the solution is, for example:
因此,我们需要多次调用detach()每一个pkg元素。我们需要指定一些其他参数来让它工作。首先是性格。仅= TRUE,它允许函数假定名称是字符串——没有它就不能工作。其次,我们可能还想卸载任何关联的名称空间。这可以通过设置unload = TRUE来实现。所以答案是,例如:
pkg <- c("package:vegan","package:permute")
lapply(pkg, detach, character.only = TRUE, unload = TRUE)
Here is a full 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, study the code in sessionInfo()
to see how it identifies what it labels as "other attached packages:". Combine that bit of code with the idea above in a single function and you are home and dry. I'll leave that bit up to you though.
如果您想将其转换为函数,请研究sessionInfo()中的代码,看看它是如何将哪些内容标记为“其他附加包:”的。将这段代码与上面的想法结合到一个函数中,您就可以大干一场了。我把这一点留给你来决定。
#4
17
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:
或许有必要增加罗曼·弗朗索瓦(Romain Francois)提供的解决方案。当加载包时,GitHub上当前可用的nothing将卸载所有加载的包;正如罗曼提供的例子:
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包:
devtools::install_github("romainfrancois/nothing")
pacman
An alternative approach uses pacman
package available through CRAN:
另一种方法是使用可通过CRAN获得的pacman包:
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
#5
8
Building on Gavin's answer but not quite to a full function would be this sequence:
以Gavin的答案为基础但不完全是一个完整的函数会是这样的序列:
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)
#6
3
or if you have RStudio, simply uncheck all the checked boxes in Packages Tab to detach
或者,如果您有RStudio,只需取消对Packages选项卡中要分离的所有复选框的选中
#7
1
Most of the times its the plyr
vs dplyr
issue. Use this in the beginning of the code:
大多数时候是plyr和dplyr的问题。在代码的开头使用这个:
detach("package:plyr", unload=TRUE)
So whenever the script runs, its clears the plyr
package
所以无论脚本何时运行,它都会清除plyr包