Reputation: 363
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?
Upvotes: 3
Views: 7895
Reputation: 6750
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.
Upvotes: 3
Reputation: 41260
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")
Upvotes: 0