在努力解决另一个问题时,我遇到了这个问题:
我可以通过以下方式删除所有 R 对象:
rm(list = ls(all = TRUE))
是否有等效的命令可以在工作 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)
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))
对于对象,期望它不会删除附加的基础包
谢谢;
最佳答案
所以,有人应该简单地回答以下问题。
lapply(paste('package:',names(sessionInfo()$otherPkgs),sep=""),detach,character.only=TRUE,unload=TRUE)
(编辑:2019 年 6 月 28 日) 在最新版本的R 3.6.0中请改用。
invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE))
请注意,使用invisible(*) 不是必需的,但对于防止 NULL 回复垂直向 R 窗口发送垃圾邮件很有用。
(编辑:2019年9月20日)在版本3.6.1中
首先将仅加载的 names(sessionInfo()$loadedOnly)
转换为显式附加的包,然后分离这些包可能会有所帮助。
lapply(names(sessionInfo()$loadedOnly), require, character.only = TRUE)
invisible(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE, force=TRUE))
可以尝试通过 $basePkgs 卸载基础包,也可以尝试使用 unloadNamespace(loadedNamespaces())
。然而,这些通常充满错误,并且可能会破坏基本功能,例如导致 sessionInfo()
仅返回错误。发生这种情况通常是因为原始封装设计缺乏可逆性。例如,目前 timeDate
可能会不可逆转地中断。
(编辑:2020 年 9 月 24 日)版本 4.0.2 下面首先加载要测试的包,然后给出完全分离除包“base”和“utils”之外的所有包的顺序。强烈建议不要分离这些包。
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).
关于r - 在 R 中工作时分离所有包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7505547/