Install a local R package with dependencies from CRAN mirror - r

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?

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.

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.

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.

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.

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.

Related

install dependencies from CRAN for a package installed from source

I want to give a beta version of my package to a coworker for testing purposes.
I build the package and put the .zip file on the Network Folder.
The Problem is, how to tell R that it should install the package from the ZipFile but its dependencies from CRAN?
install.packages("path\\to\\zipfile.zip", source = TRUE, repos = NULL)
Because repos = null disables the dependency resultion. If under stand the documentation correctly:
dependencies
logical indicating whether to also install uninstalled packages which these packages depend on/link to/import/suggest (and so on recursively). Not used if repos = NULL. Can also be a character vector, a subset of c("Depends", "Imports", "LinkingTo", "Suggests", "Enhances").
I would like to give the coworker a short snippet of code to install for himself without him having to interact further with the installation.
As the Imports or dependencies might change in later development, i would prefer not to hardcode them.
I found devtools::install() which satisfies this requirement. Although it needs devtools.

Preventing repeated package installation, or pre installing packages in R

I have a R script that I call from python using rpy2. It uses dplyr, doBy, and ggplot2. The script has install.packages commands for these 3 packages. Even thought the packages are already installed it still downloads, builds, and installs them, which is very time consuming. Is there a way to have it only do the install if the package is not already installed?
Also, I run in a docker container, so after the container is instantiated the packages are not there the first time the script runs. Is there a way to pre load the packages, in which case I would not need the install.packages commands for these packages and my above question would become moot.
I always use:
if (!require(package)) install.packages("package")
So if the package isn't available in the library, it will be installed.
install.packages( setdiff(required_packages, installed.packages()[,"Package"]) )
If you define required_packages as a character vector of the names of the packages you need, this line will only install the packages you don't currently have.
So for your case:
required_packages <- c("dplyr", "doBy", "ggplot2")
install.packages( setdiff(required_packages, installed.packages()[,"Package"]) )

R "prob" package doesn't install - failing dependencies

R version 3.4.2,
RStudio 1.1.383,
Windows 10
CRAN removed the "prob" package for R, which several open source probability textbooks depend upon.
What I did to solve this:
Another user suggested R 2.9. However,
R 2.9 breaks Rstudio,
prob isn't actually in the CRAN repository for 2.9, and
none of the versions of fAsianOptions available in the archive
install for it, anyway. (Already tried all of these.)
Investigating several topic areas in Rdocumentation turned up nothing. This used to be a popular package and other works were written that depend upon it. Now that it is broken/gone, what is the workaround?
Install dependencies first.
install.packages("installr") # Not appropriate for Macs
library(installr)
install.Rtools()
Download the latest fAsianOptions...tar.gz from the Archives
Dependencies are listed in the ../prob/DESCRIPTION file (which has no extension but is a simple text file). If these are all installed then:
install.packages("~/Downloads/fAsianOptions", repo=NULL,type="source")
That does need compilation so you needed to have the proper development tools for Windows. Note that it, too, has dependencies, so install them as well:
# In the DESCRIPTION file for fAsianOptions you read ->
# Depends: R (>= 2.4.0), timeDate, timeSeries, fBasics, fOptions
# If you had not installed all the dependnecies you would need:
install.packages("~/Downloads/fAsianOptions", dependencies=TRUE,
repo=NULL, type="source")
The combinat package can be installed from CRAN:
install.packages("combinat")
Then when your dependencies are satisfied (and you know where your ../prob-directory is in your filesystem):
install.packages("~/Downloads/prob", repo=NULL,type="source")
# obviously something else should be substituted for `~/Downloads/`
Here's the solution, thanks to 42- above.
So, for others who might encounter this same situation:
Install RTools from here: https://cran.r-project.org/bin/windows/Rtools/Rtools34.exe
Trying to install Rtools from inside RStudio will result in an "it's not available for 3.4.2" message.
Install the following dependencies: timeDate, timeSeries, fBasics, fOptions
Download fAsianOptions from here: https://cran.r-project.org/src/contrib/Archive/fAsianOptions/fAsianOptions_3010.79.tar.gz
and prob from here: https://cran.r-project.org/src/contrib/Archive/prob/prob_1.0-0.tar.gz
Unzip each into its own directory.
Use this to install each, as appropriate: install.packages("~/Downloads/fAsianOptions", dependencies=TRUE, repos=NULL, type = "source")

Possible to install a package without installing dependencies?

Is it possible to install a package without installing dependencies?
When run the following command:
install.packages("package",dependencies=FALSE)
if the dependencies are not installed beforehand, isn't it that the installation of the package fails?
My question comes from this post Install a local R package with dependencies from CRAN mirror. Why does it say installing a local package without installing dependencies?
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.
Thanks!
You cannot install and get a package to work without its dependencies. The dependencies= parameter is really an indicator if you would like R to automatically install the dependencies. If set to FALSE, R still stop and warn you so you can decide what you would like to do; if TRUE, R will automatically try to download from your current CRAN repository mirror. With repos=NULL (a local file install) there is nowhere else to look for dependencies so the dependencies= parameter is ignored.

Only download sources of a package and all dependencies

I am wondering if there's a way to use install.packages() or other related functions to do the following: only download the sources (i.e. tar.gz files) of the specified packages and all their dependencies into a specified folder (on Windows).
One reason to do this is: say I have a Linux account that is not enabled for internet access. In order to install the packages on the Linux machine, I would first download all the needed sources on my Windows machine, then ftp them over to the Linux machine, and install them on the Linux machine using
install.packages('/home/me/R/Packages/blah.tar.gz', repos = NULL)
I recently had a problem where I wanted to download all dependencies and I've solved it thus:
Say I want all the dependencies and imports of ggplot2 and MASS:
getPackages <- function(packs){
packages <- unlist(
tools::package_dependencies(packs, available.packages(),
which=c("Depends", "Imports"), recursive=TRUE)
)
packages <- union(packs, packages)
packages
}
packages <- getPackages(c("ggplot2", "MASS"))
I can now download the packages to another directory.
download.packages(packages, destdir="whereyouactuallywantthefiles",
type="source")
From there if you want to make a local repo on your Linux PC, follow the instructions here.
Try download.packages(c("xts", "rms"), "c:/TEMP", .....) instead of install.packages(); you can directly give it a target directory in the 2nd argument.
Edit several years later: As stated above on other answers and comments, by now several helper functions have been added to R's tools and utils packages. R 3.4.0 will have tools::CRAN_package_db() to download the top-level PACKAGES.rds file (and of course you could just combine download.file() and readRDS() for that too).
There are now better options for this in the tools package that comes with base R: package_dependencies(). See for example, the Answer from #sebastian-c and this recent Q&A for a related use-case.
There is an unexported getDependencies() function in the utils package. I haven't studied how it works, but combining that with #Dirk's Answer should get you most of the way there.
Basically though, it appears you use it like:
utils:::getDependencies(pkgs, dependencies, available, lib)
where pkgs is the character vector of packages to install, dependencies is a character vector of types of dependencies (Depends, Enhances etc) that you want, available is the output from available.packages() and lib is the library location for the packages within which dependencies are evaluated.
If you debug install.packages() it is basically doing the getDependencies() step then #Dirk's download.packages() step before it actually starts installing anything.

Resources