Kevin O'Neill PhD candidate in cognitive neuroscience at Duke University working with Dr. Felipe De Brigard and Dr. John Pearson on causal reasoning.

# Blogging with R markdown and GitHub Pages

If you’re making a blog post on R-related content, you’re probably going to do it using R markdown (Rmd). However, you may have noticed that our website runs on GitHub Pages, which uses Jekyll to put the site together. That creates a slight difficulty: Jekyll doesn’t know what to do with R markdown files, so we need to render our R markdown files into regular-old markdown to make things work.

# Here’s what we need to do

### 1. Set up the header

First, we need to tell R how to turn our Rmd file into a md file. We do that by adding the following to the YAML header of our Rmd file:

 1 2 3 4 output: md_document: variant: gfm preserve_yaml: TRUE

Again, this just tells R that we want to make a GitHub-flavored markdown document that retains all the other stuff laying around in our header.

### 2. Set up knitr

Next, we need to tell R exactly where and how to render our Rmd file. We do this by adding the following R chunk to our Rmd file, right after the header:

 1 2 3 4 5 6 7 {r setup, include=FALSE, cache=FALSE} knitr::opts_knit$set(base.dir="../", base.url="/") knitr::opts_chunk$set(fig.path="assets/images/YYYY-MM-DD-post-name/", cache.path = 'cache/', message=FALSE, warning=FALSE, cache=TRUE, echo=TRUE) 

You can change the last four options if you like, but these are pretty good default settings.

### 4. Put your files in the right places

Next, we need to put our files into our local clone of the dukeneuromethods.github.io repository. Specifically, we should put our Rmd files and data files into the _source directory, named with the following convention: YYYY-MM-DD-post-name.Rmd. Next, we should put any included images into a new directory assets/images/YYYY-MM-DD-post-name/, and update any links to those images as /assets/images/YYYY-MM-DD-post-name/image.png.