22

Just confirming: If I distribute my R package as ZIP/TAR then installing the package will not automatically download/install dependencies because I have to set repos = NULL in install.packages() and dependencies parameter is not used if repos = NULL? The way to possibly get this to work is to package an install script. Is that possible? Am I completely missing something here and there is a mechanism to install from source AND automagically download and install dependencies?

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453
Suraj
  • 35,905
  • 47
  • 139
  • 250

4 Answers4

10

The devtools package has a function install. If used on a directory containing the source code for an R package, it will install that package and download any dependencies from CRAN.

sebastian-c
  • 15,057
  • 3
  • 47
  • 93
  • 4
    Since the code to do this has already been written, it would sure be nice if R, and it's defacto tooling (`R CMD` / Rstudio), did some of these things by default. – blong Nov 21 '16 at 05:07
9

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.

Aaron left Stack Overflow
  • 36,704
  • 7
  • 77
  • 142
  • clever! Can my own repository be password protected or otherwise ensure private access only (to clients outside of my firewall) – Suraj Apr 27 '11 at 13:53
  • I really don't know, sorry. Mine is public. I know R gets the packages over http, so if you can limit web access to the approved clients, that should also limit access to your repository. – Aaron left Stack Overflow Apr 27 '11 at 13:58
  • thanks for the suggestion! I'm going to avoid the repository approach because of the security issues. My packages are tied to my company. It would be too much work to maintain an IP whitelist. I'll leave this question open for a few days to see if there are any other solutions – Suraj Apr 28 '11 at 06:02
  • 1
    Maybe you could make a really basic package that just has the dependencies you need and make that public. Install that first, get the necessary dependencies, and then install your real package from the file. – Aaron left Stack Overflow Apr 28 '11 at 06:13
  • 1
    another clever solution! I'll have to consider that. Ideally I could just have an installation script that calls the package install function for each package (if not already installed) but I don't know if R packages support installation scripts. Not basic stuff, but probably not too hard either. – Suraj Apr 28 '11 at 06:34
  • This answer is good one, just lacks an example. You don't have to publish your private pkg. You can create local repo. @Suraj see my answer here https://stackoverflow.com/a/74006901/2490497 – jangorecki Oct 10 '22 at 00:51
5

You can use

  devtools::install_local(path)

It can automatically download all the dependencies.

Yukun
  • 315
  • 4
  • 15
0

If you have a Github account myname, push your R package to a repo mypackage. Then just call devtools::install_github("myname/mypackage"). Package mypackage will be downloaded and installed as will all the dependencies listed under Imports in the DESCRIPTION file.