Search code examples
rpackrat

Packrat with local binary repository


I want to use packrat on a Windows 7 machine with no internet connection. I have downloaded all binary packages from http://cran.r-project.org/bin/windows/contrib/3.1/ into the local folder C:/xyz/CRAN_3_1.

The problem is now that

packrat::init(options=list(local.repos="C:/xyz/CRAN_3_1"))

throws a bunch of warnings and errors like

Warning: unable to access index for repository http://cran.rstudio/bin/...
Warning: unable to access index for repository http://cran.rstudio/src/...
Fetching sources for Rcpp (0.11.4) ... Failed
Package Rcpp not available in repository or locally

As it seems packrat tries to find

  1. the binary version of Rcpp on CRAN (fails since there is no internet connection)
  2. the source of Rcpp on CRAN (fails since there is no internet connection)
  3. the local source of the package (fails since I only have the binaries)

What I don't understand is why packrat does not also search for the local binary package...

Question 1: I could download the source CRAN repository to get around this problem. But I would like to know from you guys whether there is an easier solution to this, i.e., whether it is possible to make packrat accept a local binary repo.

Question 2: When I create my own package myPackage with packrat enabled, will the myPackage-specific local packrat library also be included in the package? That is, assume that I give the binary myPackage zip File to one of my colleagues who does not have one of the packages that myPackage depends on (let's say Rcpp). Will Rcpp be included in myPackage when I use packrat? Or does my colleague have to install Rcpp himself?


Solution

  • I managed to hack around this problem. Please bear in mind that I have never used packrat before and that I do not know its "proper" behaviour. But my impression is that the hack works. Here is how I did it:

    1. Open your project, load packrat via library(packrat)
    2. type fixInNamespace("snapshotImpl",ns="packrat") - a window opens - copy its content into the clipboard
    3. Go to /yourProjDir/ and create a file snapshotImplFix.R
    4. Copy the clipboard's content into this file ...
    5. ... but change the first line to

      snapshotImplFix=function (project, available = NULL, lib.loc = libDir(project), dry.run = FALSE, ignore.stale = FALSE, prompt = interactive(), auto.snapshot = FALSE, verbose = TRUE, fallback.ok = FALSE, snapshot.sources = FALSE)

      Note snapshot.sources = FALSE! Save and close the file.

    6. Create /yourProjDir/.Rprofile and add

      setHook(packageEvent("packrat","onLoad"),function(...) { source("./snapshotImplFix.R"); tmpfun=get("snapshotImpl",envir=asNamespace("packrat")); environment(snapshotImplFix)=environment(tmpfun); utils::assignInNamespace(x="snapshotImpl",value=snapshotImplFix,ns="packrat");})

      Points 2-6 fix the problem with the snapshot.sources argument being TRUE by default (I did not find a better way to change that...)

    7. Finally, we have to tell packrat to take our local repository. It's important that you have the right folder structure. Therefore I moved the repo from C:/xyz/CRAN_3_1 to C:/xyz/CRAN_3_1/bin/windows/contrib/3.1. Do not forget to run library(tools);write_PACKAGES("C:/xyz/CRAN_3_1/bin/windows/contrib/3.1"); if you also have to move your files.

    8. Open yourProjDir/.Rprofile again and add at the end

      local({r=getOption("repos");r["CRAN"]="file:///C:/xyz/CRAN_3_1";r["CRANextra"]=r["CRAN"];options(repos=r)})

      Note the 3 / right after file! Save and exit file.

    9. Close the project and re-open.

    10. Now you can execute packrat::init() and it should run without errors.

    It would be great if someone with more experience regarding packrat could give his/her input so that I can be sure that this hack works. Any pointers to proper solutions are highly appreciated, of course.