How to Tell Cran to Install Package Dependencies Automatically

Adding dependencies properly to an r package, so that they install automatically

A good strategy when it comes to developing your first packages is, in my experience, checking the work of others. The easiest way to do that is to check some of your favorite packages on Github. Here is, for example, a part of one of my DESCRIPTION files:

Depends: 
R (>= 3.3.0)
License: GPL-3
Imports:
stringi (>= 1.1.7),
data.table (>= 1.10.4.3),
methods (>= 3.3.0),
quanteda (>= 1.1.0),
scales (>= 0.5.0),
stats (>= 3.3.0),
utils (>= 3.3.0)

As you can see, every package has a minimal version (most of them are simply the versions I use but for some, I tested if older versions work). And I use Imports to mark packages and Depends only to indicate the oldest R version which I successfully tested. You should almost always use Imports or Suggests instead of Depends for packages.

Once you have set this up, you can run:

# This should point to the folder of your DESCRIPTION file    
setwd("/path/to/your/package/")
roxygen2::roxygenise(clean = TRUE)

Do not alter the NAMESPACE directly! This should be enough to install your package or put it on GitHub.

However, this is just the tip of the iceberg and it would probably be a good idea to check this post out and then read up on the details in this book.

Update: Given the comment from @Benjamin, I see that I have missed one part of your question. repos = NULL, type = "source" suppresses the installation of dependencies. A better way would be to use devtools. I'm not sure if this is the correct way, but when I already have a tarball and need to install it I use something like:

# In Windows use copy and paste directly on the file to get a link  
devtools::install_url("C:/custom_packages/my_package_0.0.0.9000.tar.gz")

When installing an R package, automatically reinstall dependencies when needed

This method is crude (especially since it will happily redownload a package multiple times), but it's the best I've come up with so far:

install.rec = function(pkg, repos = 'http://archive.linux.duke.edu/cran')
# Install a package and reinstall any dependencies that need
# to be reinstalled, recursively.
{while (T)
{message("INSTALLING: ", pkg)
out = paste0(collapse = " ",
system2("Rscript", stdout = T, stderr = T, sprintf(
"--no-init-file -e \"install.packages('%s', repos='%s')\"",
pkg, repos)))
p = regmatches(out, regexec(text = out, perl = T,
"package ‘(\\S+)’ was installed (?:before R|by an R version)"))[[1]]
if (length(p))
{p = p[2]
message("START RECURSING: ", pkg, " - ", p)
install.rec(p, repos)
message("END RECURSING: ", pkg, " - ", p)}
else
break}
message("DONE WITH: ", pkg)}

install.packages doesn't raise conditions, nor does it return errors, nor does it produce its output in a way captureable with capture.output, so we have to use a system call to see the error message. The idea comes from here.

Package dependencies when installing from source in R

You could make your own repository and set repos to be a vector of the places to look for packages, where it would start with your own repository and then include a link to a CRAN mirror. This is what I do and it works quite nicely, as then I can easily share my packages with others and update them from whatever computer I happen to be on.

How to automatically install Bioconductor package on which a CRAN package depends through a long chain of dependencies?

Because Bioconductor has twice-yearly releases, and this differs from CRAN release practices, the 'right' thing to do is to use Bioconductor tools to install your package, so BiocManager::install("metap"). It does not matter that metap is a CRAN package. BiocManager installs the correct version of the Bioconductor package for the user's version of R.

If this solution isn't palatable, then the next-best right thing to do is to adjust your dependencies to avoid direct or indirect dependencies on Bioconductor packages.

Installing missing dependencies in an R package

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")


Related Topics



Leave a reply



Submit