Alfred, RStudio, and Jekyll Workflow

Usually I work in RStudio to write my homeworks, to create presentations and of course to do statistical analysis. When I moved my blog to Github pages and started using Jekyll, I started looking for solutions to use RMarkdown also. There are some sources online showing how this can be done, like this, this, or this but I wanted some easy solutions to automate the workflow.

First we need to convert the .Rmd file we write in RStudio to a vanilla .md file in order Jekyll to handle it. Derek Feng has a great make.R file on this Github page. I used this file and changed it to accept the file name argument from the zsh script result. I put this R file into the main directory of the blog. Also I created an _Rmd folder to save the Rmarkdown files.

For Alfred, I wrote 2 workflows (using Matthew Guay’s great workflow Publish to Kirby from Alfred as my template). First one of these workflows creates an Rmd file with the date in the name along with the query typed in Alfred. The second workflow finds the newest file in the Rmd directory and converts it to a vanilla md file and saves it in posts/articles directory.

Alfred1

First workflow has 3 options in it, articles, blog, and page. These were the default layouts with So Simple Theme so I sticked with them. If you create blog or page, Alfred opens Mou for editing. I opted for Mou because it has a built in viewer. Of course, this behavior can be easily changed. Here are the Alfred scripts.

This one creates an article.Rmd file.

/* navigate to blog folder - REPLACE WITH YOUR OWN BLOG FOLDER BEFORE USE */
cd ~/Dropbox/emraher.github.io/_Rmd/


/* get date and time */
TIME=`date "+%Y-%m-%d %X"`
DATE=`date "+%Y-%m-%d"`

/* create a file called article.Rmd with query as title, along with the date */
echo "---
layout: post
title: {query}
excerpt: 
date: $TIME
categories: articles
tags: []
comments: false
share: true
---

" > $DATE-{query}.Rmd

/* Open Rmd file in RStudio */
open -a RStudio $DATE-{query}.Rmd

This one creates a blog post.

/* navigate to blog folder - REPLACE WITH YOUR OWN BLOG FOLDER BEFORE USE */
cd ~/Dropbox/emraher.github.io/_posts/blog/


/* get date and time */
TIME=`date "+%Y-%m-%d %X"`
DATE=`date "+%Y-%m-%d"`

/* create a file called article.md with query as title, along with the date */
echo "---
layout: post
title: {query}
excerpt: 
date: $TIME
categories: blog
tags: []
comments: false
share: true
---

" > $DATE-{query}.md

/* Open md file in Mou */
open -a Mou $DATE-{query}.md

And this one creates a page.

/* navigate to blog folder - REPLACE WITH YOUR OWN BLOG FOLDER BEFORE USE */
cd ~/Dropbox/emraher.github.io/

/* make folder */
mkdir {query}

/* change directory to new folder */
cd {query}

/* create a file called index.md */
echo "---
layout: page
title: {query}
permalink: /{query}/
---

" > index.md

/* Open index.md in Mou */
open -a Mou index.md

The second workflow is specific to Rmd file as I mentioned above. This is how the workflow looks like.

Alfred2

I used the following script to convert Rmd to md using make.R file.

/* navigate to blog/_Rmd folder - REPLACE WITH YOUR OWN BLOG FOLDER BEFORE USE */
cd ~/Dropbox/emraher.github.io/_Rmd

/* Find the newest file in Rmd directory */
filename=`ls -Frt | grep "[^/]$" | tail -n 1`

/* Change to make.R directory */
cd ..

/* Run make.R */
Rscript make.R $filename

since this is written in RMarkdown let’s test it also with a small part of default Rmarkdown file.

You can embed an R code chunk like this:

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

You can also embed plots, for example:

plot of chunk unnamed-chunk-6

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

It seems that RMarkdown works properly. The only problem I experienced is that the Plain Code Blocks do not work. Instead of Plain Code Blocks I used R code block with eval=FALSE. You can access my version of make.R here. You can access Alfred workflows here.