在 R 中工作时分离所有包 [英] detach all packages while working in R

查看:22
本文介绍了在 R 中工作时分离所有包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在努力解决另一个问题时,我遇到了这个问题:

我可以通过以下方式删除所有 R 对象:

rm(list = ls(all = TRUE))

是否有等效的命令可以在工作会话期间分离已安装的软件包?

<代码>>会话信息()R 版本 2.12.2 (2011-02-25)平台:i386-pc-mingw32/i386(32位)语言环境:[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附带的基础包:[1] stats graphics grDevices utils datasets 方法基础

<块引用>

需要(ggplot2)

加载需要的包:ggplot2加载所需的包:重塑加载所需的包:plyr附加包:'重塑'以下对象被package:plyr"屏蔽:round_any加载需要的包:grid加载所需的包:proto

<块引用>

sessionInfo()

R 版本 2.12.2 (2011-02-25)平台:i386-pc-mingw32/i386(32位)语言环境:[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附带的基础包:[1] grid stats graphics grDevices utils datasets 方法[8] 基地其他附加包:[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")分离(pkg,character.only = TRUE)分离错误(pkg,character.only = TRUE):无效的名称"参数另外: 警告信息:在 if (is.na(pos)) stop("invalid 'name' argument") 中:条件具有长度 >1 并且只使用第一个元素

我正在寻找的是全球性的东西:

 rm(list = ls(all = TRUE))

对于对象,期望它不会删除附加的基础包

谢谢;

解决方案

所以,应该有人简单地回答以下问题.

lapply(paste('package:',names(sessionInfo()$otherPkgs),sep="),detach,character.only=TRUE,unload=TRUE)

(6-28-19)请在最新版本的 R 3.6.0 中使用.

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

请注意,使用 invisible(*) 不是必需的,但可以用于防止 NULL 回复垂直向 R 窗口发送垃圾邮件.

(9/20/2019)在 3.6.1 版中

首先将加载的 names(sessionInfo()$loadedOnly) 转换为显式附加的包,然后再分离包,这样可能会有所帮助.

lapply(names(sessionInfo()$loadedOnly), require, character.only = TRUE)隐形(lapply(paste0('package:', names(sessionInfo()$otherPkgs)), detach, character.only=TRUE, unload=TRUE, force=TRUE))

可以尝试通过 $basePkgs 卸载基础包,也可以尝试使用 unloadNamespace(loadedNamespaces()).然而,这些通常充满错误,可能会破坏基本功能,例如导致 sessionInfo() 仅返回错误.这通常是因为原始包装的设计缺乏可逆性.例如,目前 timeDate 可以不可逆地中断.

(9/24/20)适用于 4.0.2 版下面首先加载要测试的包,然后给出一个序列以完全分离除包base"之外的所有包.和实用程序".强烈建议不要分离这些包.

 invisible(suppressMessages(suppressWarnings(lapply(c("gsl","fBasics","stringr","stringi","Rmpfr"), require, character.only = TRUE))))不可见(抑制消息(抑制警告)(lapply(名称(会话信息()$loadedOnly),要求,字符.仅=真))))会话信息()#以上是测试隐形(lapply(paste0('包:',c(stringr",fBasics")),分离,character.only=TRUE,unload=TRUE))#在上面的行中,我手动插入了我知道的包依赖项.用户必须先验地知道这一点或拥有自己的自动化#方法来发现它.如果不先删除依赖项,用户将不得不循环加载命名空间,然后分离 otherPkgs a#第二次通过.隐形(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"]名称(bspkgs.nbu)<-bspkgs.nbu抑制消息(不可见(lapply(paste0('包:',名称(bspkgs.nbu)),分离,character.only=TRUE,unload=TRUE)))#again 这将彻底删除所有包和加载的命名空间,除了基本包base";和实用程序"(强烈不推荐).

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)

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 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;

解决方案

So, someone should have simply answered 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 please use instead.

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

Note the use of invisible(*) is not necessary but can be useful to prevent the NULL reply from vertically spamming the R window.

(edit: 9/20/2019) In version 3.6.1

It may be helpful to convert loaded only names(sessionInfo()$loadedOnly) to explicitly attached packages first, and then detach the packages, as so.

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

One can attempt to unload base packages via $basePkgs and also attempt using unloadNamespace(loadedNamespaces()). However these typically are fraught with errors and could break basic functionality such as causing sessionInfo() to return only errors. This typically occurs because of a lack of reversibility in the original package's design. Currently timeDate can break irreversibly, for example.

(edit: 9/24/20) for version 4.0.2 The following first loads packages to test and then gives a sequence to fully detach all packages except for package "base" and "utils". It is highly recommended that one does not detach those packages.

    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 中工作时分离所有包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆