简体   繁体   中英

Installing missing dependencies in an R package

I'm distributing an R package that requires other packages. If any are missing I get (for example) the following error:

library(whSample) Error: package or namespace load failed for 'whSample' in loadNamespace(i, c(lib.loc, .libPaths()), versionCheck = vI[[i]]): there is no package called 'dplyr'

I have code to check and install dependencies in the whSample package, but R fails before it gets to it when it sees the import(dplyr) in NAMESPACE. Here's the first block of code when the package function is called:

  is_installed <- function(mypkg) is.element(mypkg, installed.packages()[,1])
  whInstall <- function(pkgNames){
    for(pkg in pkgNames){
      if(!is_installed(pkg)){
        install.packages(pkg, repos="http://lib.stat.cmu.edu/R/CRAN")
      }
      suppressMessages(suppressWarnings(
        library(pkg, character.only=T, quietly=T, verbose=F)))
    }
  }
  whInstall(c("magrittr","tools","purrr","openxlsx","data.table","dplyr","glue"))```

How can I get R to do these checks without running afoul of NAMESPACE?

A short answer is that you can ask people to install with

devtools::install_local("your-package.tar.gz")

What's happening behind the scene is that install.packages ignores the dependencies option when installing from a local file. It somehow assumes that the repository from which your are installing the package should also have the dependent packages. But for local files there is no repository, hence no dependency handling occurs.

When you install the package from a local file, install is going to search for dependencies on the same local path... and won't find them.
To get the CRAN dependecies automatically installed, you can use:

install.packages("devtools")
devtools::install_local("MypackageName.zip")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM