在 R 中,如何自动安装使用我自己的包的包?

In R, how to install packages used my own package automatically?

这个问题靠近以下站点:

R - Importing 'xlsx' package to my own package doesn't work

我在某台电脑上制作了一个包,名称为aa,安装正常

当我尝试在另一台计算机上安装自己的软件包时,出现以下错误消息:

==> Rcmd.exe INSTALL --no-multiarch --with-keep.source aa

* installing to library 'C:/Users/aaaaaaaaaaa/Documents/R/win-library/3.5'
ERROR: dependencies 'knitr', 'rstan', 'readxl', 'testdat', 'openxlsx', 'xlsx', 'gridExtra' are not available for package 'aa'
* removing 'C:/Users/aaaaaaaaaaa/Documents/R/win-library/3.5/aa'
In R CMD INSTALL

Exited with status 1.

在我的手上,一旦我安装了上述错误中描述的包 knitr,然后错误消息如下,包 knitr 从中消失:

==> Rcmd.exe INSTALL --no-multiarch --with-keep.source aa

* installing to library 'C:/Users/aaaaaaaaaaa/Documents/R/win-library/3.5'
ERROR: dependencies 'rstan', 'readxl', 'testdat', 'openxlsx', 'xlsx', 'gridExtra' are not available for package 'aa'
* removing 'C:/Users/aaaaaaaaaaa/Documents/R/win-library/3.5/aa'
In R CMD INSTALL

Exited with status 1.

我想知道的是我自己的包里用到的包有没有自动安装

也许你可以尝试这样更简单的方法: 在您的 .R 文件

中添加以下代码
tryCatch({
  library(knitr)
}, error = function(e) {
  install.packages("knitr")
  library(knitr)
})

或者

if("knitr" %in% installed.packages()[,1]){
  library(knitr)
}else{
  install.packages("knitr")
  library(knitr)
}