所以,有人应该简单地回答以下问题。
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).