在 R 中工作时,分离所有的包。

128

在解决另一个问题的过程中,我遇到了这个问题:

我可以通过以下方式删除所有 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))

针对对象,预期不会删除附加的基础包

谢谢;


4
你的问题很合理,但为什么不直接重新启动R呢? - Aaron left Stack Overflow
5
@Aaron因为你不应该有太多的负担 ;-) 为了通过R CMD check测试,一个包需要能够干净地卸载自己,所以R核心团队期望这是可能的并且是人们希望做的事情。 - Gavin Simpson
@Aaron,我认为有时候在某些包可能会引起干扰的情况下,让会话继续进行可能是有用的,但这些包在之前的步骤中已经被使用过了... - John Clark
7
无法将R还原为初始状态。我与约翰·钱伯斯讨论过这个问题,特别是对于S4类/方法注册而言,这是一件特别困难的事情。 - hadley
11个回答

-1
如果你遇到了包中有同名函数互相冲突的问题,你可以始终引用你所需要的那个包的命名空间。
pkg_name::function_i_want()

这是对所问问题的评论,而不是答案。 - SJ9
我应该将这个作为评论放在之前的plyr v. dplyr答案中,是否可以移动它?我还在学习这里的惯例。 - M. Wood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接