47

I would like to create a local R package repository such that users in my company can install packages from it and the system admins can update the local repo periodically. Access to the CRAN mirrors is currently denied.

Is there a simple way to do this?

MichaelChirico
  • 32,615
  • 13
  • 106
  • 186
harshsinghal
  • 3,680
  • 8
  • 34
  • 32

3 Answers3

47

Yes, either a copy of CRAN or a repo with local packages is easy to set up. Presumably you want this for Windows so do this:

  1. Create a top-level directory on your webserver, say R/
  2. Create the usual hierarchy in there: R/bin/windows/contrib/2.11. If you need to support other (earlier) releases, simply create directories 2.10, 2.9, ... next to the 2.11 directory.
  3. Place the packages you need into the directory (say, 2.11), then change into that directory and run the following command to generate PACKAGES and PACKAGES.gz files for the repository:

    tools::write_PACKAGES(".", type="win.binary")

That is all there is to it -- now you can access the repository by pointing to the address given a command such as

update.packages(repos="http://my.local.server/R", ask=FALSE)

which I even do in R/zzz.R for local packages so that they update themselves.

Edit some five+ years later: And the drat package now automates a lot of this, and shines particularly if you also use GitHub to serve the repository over http/https (but is useful for other or local hosting too).

Farid Cheraghi
  • 739
  • 1
  • 10
  • 22
Dirk Eddelbuettel
  • 347,098
  • 55
  • 623
  • 708
  • Automatic updating sounds very convenient, but I don't understand where to put the update command. I've been playing around with `zzz.R` and `.onLoad` but can't get it to pass `R CMD check ...`. In a way it seems a bit strange to update what you are currently loading. – Backlin Jan 07 '13 at 08:19
  • The update command can be type into R by anyone who can get to the repository. – Tommy O'Dell Mar 15 '13 at 01:36
  • 4
    Further to Dirk's comments, if you get the package users to edit their `Rprofile.site` files (which get run at startup of every R session) to include something like `options(repos = c(getOption("repos"), MyCompanyRepo= "http://my.local.server/R"))`, then your users can do things like `install.packages("mypackage")` without needing to point to a path. – Tommy O'Dell Mar 18 '13 at 01:19
  • Been there done that but I don't currently have a Windows box up to confirm. If you have the correct `file:/...` form (one slash, two slash, three slash, ...) it should work as we really just pass around a reference to a directory where we tell R to write and copy. So please do some tests and if you find our documentation out of what do file an issue ticket. – Dirk Eddelbuettel Mar 21 '16 at 16:16
  • @TommyO'Dell's code did not work for me straight ahead (maybe a Windows thing?.). Putting the following into `.Rprofile` did the job for my [drat](http://dirk.eddelbuettel.com/code/drat.html) repo and ensured RStudio compatibility: `options( repos = c( getOption("repos"), my_drat = "https://petermeissner.github.io/drat", CRAN = "https://cran.rstudio.com/", CRANextra = "http://www.stats.ox.ac.uk/pub/RWin" ) )` – petermeissner Aug 02 '16 at 20:40
  • Note that Q and A are from 2010 (!!) and that these days we have e.g. `drat` to modify these values. – Dirk Eddelbuettel Aug 02 '16 at 20:41
  • This is an old Q&A but extremely helpful if you can't install on the webserver. Is there documentation that explains what the top-level directory and "usual hierarchy" of a CRAN repo are now? – Josh Bradley Apr 07 '17 at 23:37
  • You can browse any web-accessible ones such as [the official one](http://cloud.r-project.org/src/contrib). Ditto for path with `/bin/...`. – Dirk Eddelbuettel Apr 07 '17 at 23:42
19

Read the section of the Administrator guide.

Shane
  • 95,736
  • 34
  • 221
  • 217
4

The package miniCRAN also provides great functionality for this. The key advantage being that you don't need a full mirror, but can setup a "mini" mirror of CRAN with only the packages distributions you need, including their dependencies.

Glen Moutrie
  • 285
  • 3
  • 8