Using checkpoint in a markdown document

Andrie de Vries

2017-04-07

Introduction

Sometimes you want to use create a report using markdown, and you want to checkpoint the code in this document.

However, running this as part of a RMarkdown process is problematic, since the knitting process runs inside a temporary folder that is different from the current working directory.

To resolve this, I propose a hacky solution: create a “manifest” file in the same folder that contains all of the library() calls.

Example

Imagine you have a small script that you want to put in an Rmarkdown document with a checkpoint.

# demo script
library(MASS)
hist(islands)

The checkpoint solution

The only way I’ve found to get checkpoint() to work inside an RMardown document, is really a bit of a hack. The workaround is to create a manifest of required packages, and write this to an R file in the working directory.

For example, imagine your R script uses the MASS package, then create a manifest file like this:

```{r, include=FALSE}
# write a manifest to local folder
cat("
library(MASS)
",
file = "manifest.R")
```

This is hacky, since it requires you to construct the list of library() calls by hand and put these into the manifest file.

(Note that you can use include=FALSE in the code block, so that this code doesn’t show up in your rendered document.)

Once this is done, the checkpoint process from here is straight-forward

```{r, include=FALSE}
# Create .checkpoint folder (in tempdir for this example)
td <- tempdir()
dir.create(file.path(td, ".checkpoint"), recursive = TRUE)

# Create the checkpoint
library(checkpoint)
checkpoint("2017-03-28", checkpointLocation = td)
```

Check that this works

Now you are ready to put these instructions in an actual code block to see what happens.

# write a manifest to local folder
cat('
library(MASS)
',
file = "manifest.R")

# Create .checkpoint folder (in tempdir for this example)
dir.create(file.path(tempdir(), ".checkpoint"), recursive = TRUE)

# Create the checkpoint
library(checkpoint)
checkpoint("2017-03-28", checkpointLocation = tempdir())
## Scanning for packages used in this project
## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |=========                                                        |  14%
  |                                                                       
  |===================                                              |  29%
  |                                                                       
  |============================                                     |  43%
  |                                                                       
  |=====================================                            |  57%
  |                                                                       
  |==============================================                   |  71%
## No file at path 'C:\Users\adevries\AppData\Local\Temp\RtmpAbBU8M\file22d44c636940.Rmd'.
## 
  |                                                                       
  |========================================================         |  86%
## No file at path 'C:\Users\adevries\AppData\Local\Temp\RtmpAbBU8M\file22d472c1453.Rmd'.
## 
  |                                                                       
  |=================================================================| 100%
## No file at path 'C:\Users\adevries\AppData\Local\Temp\RtmpAbBU8M\file22d4389e3298.Rmd'.
## - Discovered 3 packages
## Unable to parse 3 files:
## - checkpoint.Rmd
## - managing-checkpoint-archives.Rmd
## - using-checkpoint-with-knitr.Rmd
## Installing packages used in this project
##  - Installing 'MASS'
## MASS
##  - Previously installed 'knitr'
## checkpoint process complete
## ---

If this worked, you should see that the library path now points to tempdir() and that MASS should be one of only a few package installed:

.libPaths()
## [1] ".../Temp/RtmpIVB6bI/.checkpoint/2017-03-28/lib/x86_64-w64-mingw32/3.3.2"
## [2] ".../Temp/RtmpIVB6bI/.checkpoint/R-3.3.2"
installed.packages()[, "Package"]
##       MASS   compiler 
##     "MASS" "compiler"

Your real R code:

Now your real R code follows, and it creates the plot, as expected:

library(MASS)
hist(islands)

Conclusion

This is a bit of a hack, but points in a direction for getting your RMarkdown script to be checkpointed.