Creating Jekyll blog posts from R.

[This article was first published on From Guinness to GARCH, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Adam Duncan Also avilable on

Setting up a Jekyll/Jekyll Bootstrap blog site is a very worthwhile experience. Should you choose to use Jekyll as your blogging platform, you will find many resources out there describing the setup process. This post is not about getting set up using Jekyll or Jekyll Bootstrap. It’s about establishing a good workflow from R Markdown -> Jekyll Blogpost -> R-Bloggers.

If you want to contribute to the R-Bloggers website, you will need to make sure your posts are in R-blogger friendly format. This isn’t exactly trivial, but it isn’t terribly difficult either. WordPress users can use the If you generate html straight from R-Studio (a wonderful feature of R-Studio) using the “Compile html Notebook from R-Script” function you will get a very nice .html file viewable using your favorite browser. The problem is that this format is not friendly with R-bloggers. The graphics generated by the R-Studio function will be embedded in the html file in text format. It will look like a bunch of random text sequences if you examine the html code.

In order for your content to be friendly with the R-bloggers site, your graphics will need to be placed up on your web server as seperate graphics files. This can be an arduous process if you intend to manually export all of your blog post graphics. Fortunately, Jason Fisher created a really nice code snippet that takes care of a creating Jekyll blog posts from R. Jason gives a step-by-step walkthrough of embedding R code in a Jekyll blog post here.

For R-bloggers who want to post from Jekyll, you only need to do the following:

  • Create your R Markdown document with embedded code and graphics. Follow standard R Markdown syntax.
  • Save this R Markdown file on your local machine.
  • Create an new R Script and call it whatever you like. Mine is “makeBlogPostfromRMarkdown.R”
  • Minimally, this script should contain the following code:
myjekyllsite = c("")

KnitPost <- function(input, base.url = myjekyllsite) {
    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")
    knit(input, envir = parent.frame())

  • Move the resulting image folder (called ‘figs’) to your local git repository for your Jekyll site.
  • Move the resulting markdown file to your local posts folder for your Jekyll site.
  • Add YML front matter to the head of the newly generated markdown file.
  • Note: You need to set the “base url” in the code to the full blog site URL or the Feedburner feed won’t
    pick up the images in the “fig” folder.

You only need to create the R Script with the Knitpost function once. You can then just pass your .Rmd blogposts filename to your KnitPost() function and you’ll get a nice folder with all your graphics and a new .md file you can edit before publishing to the web.

Push your local changes and check out your new blog post. You should head over to Feedburner and check your feed. You should see a nice complete post with graphics, embedded code, and whatever else you put in your post. And, your new post will play nicely with R-bloggers.

If you’re feeling motivated, you could modify the KnitPost() function to automatically add the appropriate
YML front matter to the post. This would save time having to add it manually after the .md file is generated.

To leave a comment for the author, please follow the link and comment on their blog: From Guinness to GARCH. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)