rinstallationpackage-development

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


This question is near to the following site:

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

I made some package, named aa, in some computer in which installations go well.

When I try to install my own package in another computer, then the following error message occurs:

==> 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.

By my hand, once I installed the package knitr which is described in above error, then error message is the following from which the package knitr disappears:

==> 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.

What I want to know is that the packages used in my own packages are automatically installed or not.


Solution

  • Maybe you can try the easier way like this : Add the following code in your .R file

    tryCatch({
      library(knitr)
    }, error = function(e) {
      install.packages("knitr")
      library(knitr)
    })
    

    Or

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