Install a local R package with dependencies from CRAN mirror
Asked Answered
L

5

58

I have built an R package, i.e. I have the mypackage.tar.gz file. This package depends on several other packages, all downloadable and installable from any CRAN mirror.

Now I want to install this package on a system where the dependencies are not yet installed, and I would like that the dependencies will be downloaded and installed automatically when I install my package.

I tried:

install.packages("mypackage.tar.gz",type="source",dependencies=TRUE,repos="http://a.cran.mirror")

but it searches for mypackage.tar.gz on the mirror (and obviously it does not find), while if I set repos=NULL it correctly tries to install the local package file (as documented), but obviously it does not find the dependencies packages.

So my question is: is there a way to perform a 'mixed' installation (local package with online dependencies) or the only way to do is to manually install all the dependencies?

Lacework answered 29/7, 2014 at 13:57 Comment(3)
Related (and possibly duplicate): #5805549Africa
Flabbergasted that R does not provide a mechanism to do this by default. Isn't this package management 101?Conveyance
@BradSolomon R does provide the mechanism for it. See my answer.Hetzel
S
9

You could use install from the devtools package. Just run install("<directory of your package>", dependencies = TRUE). Its help states:

Uses R CMD INSTALL to install the package. Will also try to install dependencies of the package from CRAN, if they're not already installed.

Shirt answered 4/12, 2015 at 13:16 Comment(0)
C
6

Here, I'm using untar() with devtools::install() and passing in a directory to which the source tarball has been extracted.

d <- tempdir()
untar("mypackage.tar.gz", compressed="gzip", exdir=d)
devtools::install(file.path(d, "mypackage"), dependencies=TRUE,
                  repos="https://cloud.r-project.org/")

If you want to install from multiple repos, you can provide a list of them. For example, to use both Bioconductor and CRAN, you could run:

 devtools::install(file.path(d, "mypackage"), dependencies=TRUE,
                   repos=BiocManager::repositories())

NOTE: I can't figure out how to directly pass the tarball to install(), but this solution works in the meantime and leaves no clutter because we extract to a temp directory. It seems install_local() should be able to take a tarball, but I am getting an error when attempting to do so.

Cockney answered 2/3, 2018 at 14:59 Comment(1)
install_local() worked exactly as needed for me.Luau
A
4

If you already have installed your local package, you should be able to use a couple functions in tools to install the dependencies from CRAN:

library('tools')
installFoundDepends(pkgDepends('mypackage', local = FALSE)$Found)

Note: You can pass args (like repos) through installFoundDepends to install.packages.

You can also use the Depends element from the pkgDepends output to pass directly to install.packages:

install.packages(pkgDepends('mypackage')$Depends)

UPDATE: Apparently it is not possible to install a local package with dependencies=FALSE. This seems odd, since you can do that for a remote package from a repository. The reason (looking at the source code) is that if(is.null(repos) & missing(contriburl)), installation is handled via system calls to R CMD INSTALL, which has no dependency-related arguments.

Africa answered 30/7, 2014 at 13:40 Comment(3)
I can not install my local package if I don't have yet installed all its dependencies.Lacework
unfortunately it does not work, even with dependencies set to FALSE, actually the man page clearly specify that with repos set to NULL (needed to install a local package), the dependencies parameter is ignored.Lacework
@Lacework Okay, many further tests reveals this is really annoying and surprising. I actually think that being able to install local packages without dependencies is a pretty reasonable feature request for R that would be worth submitting.Africa
C
3

If you are not opposed to using another package who manages this for you, this can nowadays be easily achieved with the {remotes} package.

install.packages("remotes")
remotes::install_local("mypackage.tar.gz")

You can specify some further options which dependencies you want (e.g. also those in 'Suggests') etc.:

?remotes::install_local

{remotes} itself does not have dependencies afaik, so it does not add too much clutter to your environment.

Celestina answered 8/3, 2022 at 8:36 Comment(0)
H
2

So old question and so many answers but unfortunately none of them presents the canonical way to address the problem. R was designed to handle situations like this, no extra packages are needed. One has to create local repository, and then use it, together with CRAN url, as a repository source when installing. Below is code that present complete process.

## double check our dependency is not yet installed
## remove.packages("data.table")
"data.table" %in% rownames(installed.packages())
#[1] FALSE

## create our pkg
hello = function() "world"
package.skeleton(name="pkg", list="hello")
#...
cat("Imports: data.table\n", file="pkg/DESCRIPTION", append=TRUE)
unlink(c("pkg/Read-and-delete-me", "pkg/man"), recursive=TRUE)
rm(hello)

## publish our pkg in current working directory
system("R CMD build pkg")
#...
dir.create("src/contrib", recursive=TRUE)
file.rename("pkg_1.0.tar.gz", "src/contrib/pkg_1.0.tar.gz")
#[1] TRUE
tools::write_PACKAGES("src/contrib")

## install pkg and its dependencies automatically
install.packages("pkg", repos=c(
  paste0("file://", getwd()),
  "https://cloud.r-project.org"
))
#Installing package into '/home/jan/R/x86_64-pc-linux-gnu-library/4.2'
#(as 'lib' is unspecified)
#also installing the dependency 'data.table'
#...

## test
library(pkg)
hello()
#[1] "world

"data.table" %in% rownames(installed.packages())
#[1] TRUE

On windows one may need to specify type="source" and amend paths.

Hetzel answered 9/10, 2022 at 17:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.