在努力解决另一个问题时,我遇到了这个问题:
我可以通过以下方式删除所有R对象:
rm(list = ls(all = TRUE))
在工作会话期间是否有可以分离已安装软件包的等效命令?
> 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
需要(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
我试过这种方式,虽然它甚至不是全球解决方案:
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
我所喜欢的是全球性的:
rm(list = ls(all = TRUE))
对象,期望它不会删除附加的基础包
感谢;
答案 0 :(得分:79)
所以,有人应该简单地回答以下问题。
lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE)
答案 1 :(得分:49)
请试试这个:
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 :(得分:29)
你很亲密。请注意?detach
对name
的第一个参数detach()
所说的内容:
参数:
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’.
因此,我们需要每detach()
个元素重复调用一次pkg
。我们需要指定其他一些参数才能使其工作。第一个是character.only = TRUE
,它允许函数假设name
是一个字符串 - 如果没有它,它将无法工作。其次,我们也可能想要卸载任何相关的命名空间。这可以通过设置unload = TRUE
来实现。所以解决方案是,例如:
pkg <- c("package:vegan","package:permute")
lapply(pkg, detach, character.only = TRUE, unload = TRUE)
以下是一个完整的例子:
> 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
如果要将其转换为函数,请研究sessionInfo()
中的代码,以查看它如何标识其标记为“其他附加包:”。将这一点代码与上述想法结合在一个功能中,您就可以回家了。不过,我会把这一点留给你。
答案 3 :(得分:20)
nothing
添加Romain François提供的解决方案可能是值得的。加载时,nothing
上当前可用的包GitHub将卸载所有已加载的包;就像Romain提供的示例一样:
loadedNamespaces()
[1] "base" "datasets" "grDevices" "graphics" "methods" "stats"
[7] "utils"
require(nothing, quietly = TRUE)
loadedNamespaces()
[1] "base"
使用devtools
包:
devtools::install_github("romainfrancois/nothing")
pacman
另一种方法是使用CRAN提供的pacman
包:
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
答案 4 :(得分:8)
以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)
答案 5 :(得分:3)
或者如果你有RStudio,只需取消选中Packages选项卡中的所有复选框即可分离
答案 6 :(得分:3)
#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()
这将确保将所有软件包与基本软件包分开
答案 7 :(得分:0)
大部分时间都是plyr
vs dplyr
问题。在代码的开头使用它:
detach("package:plyr", unload=TRUE)
因此,只要脚本运行,它就会清除plyr
包
答案 8 :(得分:0)
将各种答案中的一些内容相结合,可以提供我能找到的最可靠的解决方案...
packs <- c(names(sessionInfo()$otherPkgs), names(sessionInfo()$loadedOnly))
if(length(packs) > 0){
message('Unloading packages -- if any problems occur, please try this from a fresh R session')
while(length(packs) > 0){
newpacks <- c()
for(packi in 1:length(packs)){
u=try(unloadNamespace(packs[packi]))
if(class(u) %in% 'try-error') newpacks <- c(newpacks,packs[packi])
}
packs <- newpacks
Sys.sleep(.1)
}
}
答案 9 :(得分:0)
为什么不删除所有附加的包?
intialPackages = search() # added as 1st line of R script to get list of default packages
# below lines are added when newly attached packages needs to be removed
newPackages = search()[!(search() %in% intialPackages)]
try(sapply(newPackages, detach, character.only=TRUE, unload=TRUE, force=TRUE), silent=TRUE)
答案 10 :(得分:-1)
如果您的软件包在名称上相互冲突的程序包遇到问题,则始终可以引用您要使用的函数的程序包的名称空间。
pkg_name::function_i_want()