Install a local R package with dependencies from CRAN mirror

限于喜欢 提交于 2019-11-28 17:25:40

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.

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.

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=BiocInstaller::biocinstallRepos())

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.

I personally use RStudio which tells you which dependencies are missing. Then I copy the string in the arguments of the following small script to change the 'strange' symbols in classical " (xclip is copying to clipboard [it is like pbcopy on macOS]).

#!/bin/bash
echo $@ | sed 's/‘/"/g' | sed 's/’/"/g' | xclip -selection clipboard

Then I simply use install.packages(c(ctrl_v__what_to_install)) and R starts to install all the dependencies.

NB: remember that the two written in the above script are different and the first time you copy this script, I advise to copy again the original quotation marks characters.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!