当一个包依赖于另一个时,自动加载2个包

时间:2014-12-11 20:03:15

标签: r package

我有两个R套餐,foobarbar取决于foo,因此在其描述文件中,它具有行Depends: foo。我想在每次开始使用R时自动加载这两个包。因此,我在.Rprofile中写了以下内容:

options(defaultPackages = c([various other packages], "foo", "bar")

其中[various other packages]只是我自动加载的各种其他包的占位符,例如stats

当我启动R时,这很好用。但是,以下情况:当我尝试重新安装foo

时发生错误
Error: package ‘foo’ is required by ‘bar’ so will not be detached

这会暂停安装过程,并且无法通过任何更改成功重新安装foo。我可以毫无问题地重新安装bar

是什么导致了这个错误,有没有办法解决它,同时仍然自动加载两个包。

1 个答案:

答案 0 :(得分:0)

错误是由于R仍然在环境中加载了包,我使用Agreement包中列出了R2HTML取决于以下示例:

> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> install.packages("Agreement", lib = .libPaths()[2], repos = "http://dirichlet.mat.puc.cl/", quiet = TRUE, verbose = FALSE)
package ‘Agreement’ successfully unpacked and MD5 sums checked
> library("Agreement")
Loading required package: R2HTML
Warning messages:
1: package ‘Agreement’ was built under R version 3.1.2 
2: package ‘R2HTML’ was built under R version 3.1.2 
> search()
 [1] ".GlobalEnv"        "package:Agreement" "package:R2HTML"   
 [4] "package:stats"     "package:graphics"  "package:grDevices"
 [7] "package:utils"     "package:datasets"  "package:methods"  
[10] "Autoloads"         "package:base"     
> install.packages("Agreement", lib = .libPaths()[2], repos = "http://dirichlet.mat.puc.cl/", quiet = TRUE, verbose = FALSE)
Warning: package ‘Agreement’ is in use and will not be installed
> install.packages("R2HTML", lib = .libPaths()[2], repos = "http://dirichlet.mat.puc.cl/", quiet = TRUE, verbose = FALSE)
Warning: package ‘R2HTML’ is in use and will not be installed

复制了您的错误,因为仍然附加了包。一个简单的解决方法是在再次安装之前手动卸载这个单行程序:

> for(k in c("Agreement", "R2HTML")) while(paste0("package:", k) %in% search()) detach(paste0("package:", k), unload = TRUE, character.only = TRUE)
> search()
[1] ".GlobalEnv"        "package:stats"     "package:graphics" 
[4] "package:grDevices" "package:utils"     "package:datasets" 
[7] "package:methods"   "Autoloads"         "package:base"     
> install.packages("Agreement", lib = .libPaths()[2], repos = "http://dirichlet.mat.puc.cl/", quiet = TRUE, verbose = FALSE)
package ‘Agreement’ successfully unpacked and MD5 sums checked

此安装包含R2HTML,但如果您想确定,那么您也可以运行以下内容:

> install.packages("R2HTML", lib = .libPaths()[2], repos = "http://dirichlet.mat.puc.cl/", quiet = TRUE, verbose = FALSE)
package ‘R2HTML’ successfully unpacked and MD5 sums checked