Site icon R-bloggers

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 R-bloggers.com

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:

myjekyllsite = c("http://YOURBLOGNAME.github.io/")

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

KnitPost("YOURNEWPOST.Rmd")

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.

R-bloggers.com 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.