install dependencies from CRAN for a package installed from source - r

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.

Related

Install dependencies of custom package

I have developed a local package that depends on others available on CRAN (as an example, pool). Therefore, when I attempt to install the package using the standard
install.packages("/path/to/package",
repos = NULL,
type = "source")
I get an error because the dependencies aren't installed. install.packages has an argument dependencies which by default would try to install those dependencies. However, as stated by the man page (and commented in the linked question below), repos = NULL means the dependencies are ignored.
To get around that, I used package miniCRAN to create a repo containing my package, hoping I could do a repos = c("myRepo", getOption("repos")) to get it to work.
Now I can install my package using
install.packages("package",
repos = c("/path/to/repo", getOptions("repos"),
type = "source")
But only if I've already installed pool. If not, I still get an error because it can't find the dependencies.
So I called miniCRAN::addPackage("pool"), which adds that package and its many dependencies to my repo, and they all appear if I call miniCRAN::pkgAvail().
However, if I attempt to install my package again, I still get a there is no package called 'pool' error.
Interestingly, if I try to install pool itself from the repo, it works.
install.packages("pool",
repos = "/path/to/repo",
type = "source")
install.packages("package",
repos = "/path/to/repo",
type = "source")
Obviously, however, this kind of beats the point of adding pool to the repo: I might just as well have installed it from CRAN.
So what's going on here, and is this really the only way to install local packages and their CRAN dependencies?
Figured it out.
The problem was a misunderstanding on my part regarding roxygen, which I've been using for my documentation. I assumed it handled the Imports: section of the DESCRIPTION file, which it doesn't ((1), (2)). So while the NAMESPACE file has all the necessary importFrom(pool, ...) calls, pool wasn't actually on my DESCRIPTION.
After fixing that oversight, using remote::install_local("path/to/pkg") (or devtools::install()) ((3)) worked: it installed my package and pulled its dependencies from CRAN.

Loading dependencies from package-internal packrat library

I am building an R package with packrat. The package is fully tested and installation from the locally saved source file by
install.packages("myPackage.tar.gz", repos = NULL, type = "source")
works if all dependencies (specified in the Imports: field) are installed on the local machine.
However, I would like to install that package on another server where dependencies are not installed. When I try to do this, I get the error
"ERROR: dependencies ‘survey’, ‘dplyr’ are not available for package 'myPackage'"
I also tried to install the packrat bundle which I created by calling
packrat::bundle(project = 'pathtomypackageproject', file = 'myPackage.tar.gz',
include.lib = TRUE)
but I get the same error.
I think the problem is that, upon installing 'myPackage', R searches the first element of .libPaths(), doesn't find anything and since "repos = NULL" is specified, has nowhere to install the packages from so the error is thrown.
A solution I'm still trying to avoid is to transfer a repository containing all dependencies to the server and pointing to the repository when installing the package. Ideally, I only have to transfer myPackage.tar.gz.
My question is if there is some way to point to the internal packrat library, where all dependencies can be found, and import the namespaces from there.
If you have included the list of packages to be imported in DESCRIPTION File, then you just need to do this while installation of your package:
install.packages("myPackage",dependencies=TRUE)

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.

installing an archived package

The package is available on this website.
http://cran.r-project.org/src/contrib/Archive/rlandscape/
When I use:
install.packages("rlandscape",
repos = "http://cran.r-project.org/src/contrib/Archive/rlandscape/",
type="source")
I get the following error:
package 'rlandscape' is not available (for R version 3.1.2)
I have tried older versions too but no luck..
The devtools package has a function that can install archived versions. Try:
library("devtools")
install_version("rlandscape",version="1.0",
repos="http://cran.r-project.org")
(You should be able to use repos=getOption("repos")["CRAN"] instead, but it looks like your repos option is slightly messed up, i.e. the URL is missing the http://.)
(The repos argument is necessary to work around what I think is a glitch in install_version, i.e. it assumes that repos is a length-1 character vector.)
This should, I think, also automatically install appropriate dependencies -- although it's a bit of a catch-22 if they are in the CRANextra repository for Windows, since that has to be suppressed in order to get install_version to work ...
It may also be the case that install_version automatically assumes that you want the package and all dependencies installed as source (not binary) installations, in which case you will need to have compilation tools installed. The rlandscape package doesn't actually have any compiled code included, but its dependencies do ...
This is an old (archived) package that is no longer supported. If you really need it, you can install it using R CMD INSTALL but you need also to install all its dependencies manually.
Installing your desired package gave me the following:
>R CMD INSTALL ~/Downloads/rlandscape_1.0.tar.gz
* installing to library ‘/Users/mohamedahmed/Rlibs’
ERROR: dependencies ‘spatstat’, ‘deldir’, ‘gWidgets’, ‘gWidgetsRGtk2’ are not available for package ‘rlandscape’
* removing ‘/Users/mohamedahmed/Rlibs/rlandscape’
I am not sure all dependencies are still available on CRAN, but it seems to be challenging task.

Install a local R package with dependencies from CRAN mirror

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.

Resources