rpackrat

R: instructions for unbundling and using a packrat snapshot


I used packrat (v 0.4.8.-1) to to create a snapshot and bundle of the R package dependencies that go along with the corresponding R code. I want to provide the R code and packrat bundle to others to make the work I am doing (including the R environment) fully reproducible.

I tested unbundling using a different computer from the one I used to write R code and create the bundle. I opened an R code file in R studio, and called library(packrat) to load packrat (also v 0.4.8-1). I then called packrat::unbundle(bundle = "directory", where = "directory"), which unbundled successfully. But subsequently calling packrat::restore() gave me the error "This project has not yet been packified. Run 'packrat::init()' to init packrat". It seems like init() should not be necessary because I am not trying to create a new snapshot, but rather utilize the one in the bundle. The packrat page (https://rstudio.github.io/packrat/) and CRAN provide very little documentation about unbundling to help troubleshoot this, or that I could point users of my code to for instructions (who likely will be familiar with R, but may not have used packrat).

So, can someone please provide clear step-by-step instructions for how users of a bundled snapshot should unbundle, and then use that saved snapshot to run a R code file?


Solution

  • After some experimenting, I found an approach that seems to have worked so far.

    I have provided users with three files:

    -tar.gz (packrat bundle file)
    -unbundle.R (R code file that includes a library statement to load 
     the packrat library, and the unbundle command for the tar.gz file)
    -unbundle_readme.txt  
    

    The readme file includes instructions similar to those below, and so far users have been able to run R code using the package dependencies. The readme file tells users about requirements (R, R studio, packrat, R package development prerequisites (Rtools for Windows, XCode for Mac)), and includes output of sessionInfo() to document R package versions that the R code should use after instructions are followed. In the example below 'code_folder' refers to a folder within the tar.gz file that contains R. code and associated input files.

    Example unbundle instructions:

    Step 1

    Save, but do not expand/unzip, the tar file to a directory. Problems with accessing the saved package dependencies are more likely when a program other than R or R studio is used to unbundle the tar file.

    If the tar file has already been expanded, re-save the tar file to a new directory, which should not be a the same directory as the expanded tar file, or a subdirectory of the expanded tar file.

    Step 2

    Save unbundle.R in the same directory as the tar file

    Step 3

    Open unbundle.R using R studio

    Step 4

    Execute unbundle.R (This will create a subfolder ‘code_folder’. Please note that this step may take 5-15 minutes to run.)

    Step 5

    Close R studio

    Step 6

    Navigate to the subfolder ‘cold_folder’

    Step 7

    Open a R script using R studio (The package library should correspond to that listed below. This will indicate R studio is accessing the saved package dependencies.)

    Step 8

    Execute the R code, which will utilize the project package library. After the package library has been loaded using the above steps, it is not necessary to re-load the package library for each script. R studio will continue to access the package dependencies for each script you open within the R studio session. If you subsequently close R-studio, and then open scripts from within the unbundle directory, R studio should still access the dependencies without requiring re-loading of the saved package snapshot.