David Robinson bio photo

David Robinson

Data Scientist at Stack Overflow, works in R and Python.

Email Twitter Github Stack Overflow

Subscribe


Recommended Blogs

Workflow for Creating Pages

I use knitr and jekyll to produce these reproducible R pages. Here are the steps:

  • Set up jekyll-bootstrap, optionally with a GitHub repository.

  • Set up a folder called _R to contain your .Rmd files that will be compiled.

  • Put the following script into knitpages.R. When run, this will regenerate pages in the pages directory from any newer files in the _R directory:

#!/usr/bin/Rscript --vanilla

# compiles all .Rmd files in _R directory into .md files in Pages directory,
# if the input file is older than the output file.

# run ./knitpages.R to update all knitr files that need to be updated.

KnitPost <- function(input, outfile, base.url="/") {
    # this function is a modified version of an example here:
    # http://jfisher-usgs.github.com/r/2012/07/03/knitr-jekyll/
    require(knitr);
    opts_knit$set(base.url = base.url)
    fig.path <- paste0("figs/", sub(".Rmd$", "", basename(input)), "/")
    opts_chunk$set(fig.path = fig.path)
    opts_chunk$set(fig.cap = "center")
    render_jekyll()
    knit(input, outfile, envir = parent.frame())
}

for (infile in list.files("_R", pattern="*.Rmd", full.names=TRUE)) {
    outfile = paste0("pages/", sub(".Rmd$", ".md", basename(infile)))

    # knit only if the input file is the last one modified
    if (!file.exists(outfile) |
        file.info(infile)$mtime > file.info(outfile)$mtime) {
        KnitPost(infile, outfile)
    }
}
  • Create a .Rmd file in the _R directory, such as _R/example.Rmd. Run the command

     ./knitpages.R
    

(To-do: have the script be run as part of the Jekyll updating process).


Now, some R code just for fun:

summary(cars)
##      speed           dist    
##  Min.   : 4.0   Min.   :  2  
##  1st Qu.:12.0   1st Qu.: 26  
##  Median :15.0   Median : 36  
##  Mean   :15.4   Mean   : 43  
##  3rd Qu.:19.0   3rd Qu.: 56  
##  Max.   :25.0   Max.   :120
plot(cars)

center