(Semi-)automating the R markdown to blogger workflow

January 2, 2013
By

(This article was first published on metvurst, and kindly contributed to R-bloggers)

In his recent post 100 most read R posts for 2012 (stats from R-bloggers) – big data, visualization, data manipulation, and other languages Tal Galili - the guy behind R-Bloggers - presents his wishlist for 2013. Among other things he states

“The next step will be to have a “publish to your WordPress/blogger” button right from the RStudio console – allowing for the smoothest R blogging experience one could dream of.

I hope we’ll see this as early as possible in 2013.”

Given that I had some troubles myself recently with finding a convenient way of creating post for this blog (a blogspot site) and that I didn't want to wait until RStudio may integrate such functionality, I decided to come up with my own work around for the time being.

But let's start from the beginning:

What I want:

  • I want to blog
  • I want to blog using RStudio's R markdown framework (as it is very convenient)
  • I want to have code highlighting in my posts

What I don't want:

  • I don't want to manually fiddle around in the html code for each of the posts I create with RStudio
  • I don't want write blog posts using the compose section on blogger
  • Basically, I don't want to spend a lot of time on formatting (I rather spend the time on coding)

Why I want this:

  • For starters, because I'm lazy
  • Secondly, I want to integrate blogging into my teaching in the near future and for that I need a convenient and straight forward solution on how to create nice posts (after all I want to teach the students how to use R, not html)

The solution (as of now) is as follows (at least for me):

Yihui Xie has produced a SyntaxHighlighter Brush for the R Language which can be used for highlighting R code in blog posts.

In order to get the SyntaxHighlighter working on your blog, simply follow this tutorial at thebiobucket*.

However, the SyntaxHighlighter by Alex Gorbatchev, and subsequently also Yihui's brush for R, works a little different from the knitr code highlighting implementation. Basically, a chunk of code created with knitr is prepended by this little bit of html code:

<pre><code class="r">

whereas SyntaxHighlighter requires the following format for it to work:

<pre class="brush: r">

In order to automate the process of adjusting the brush definition needed for code highlighting using the SyntaxHighlighter brush I created the follwing function:

rmd2blogger <- function(rmdfile) {

  stopifnot(require(knitr))

  ## knit rmdfile to <body> - only html
  knit2html(rmdfile, fragment.only = TRUE)

  ## read knitted html file
  htm.name <- gsub(".Rmd", ".html", rmdfile)
  tmp <- readLines(htm.name)

  ## substitue important parts
  tmp <- gsub("<pre><code class=\"r\">", "<pre class=\"brush: r\">", tmp)
  tmp <- gsub("</code></pre>", "</pre>", tmp)

  ## write with .blg file ending
  writeLines(tmp, paste(htm.name, ".blg", sep =""))

}

This function has two important components to it:

  1. it uses knitr's knit2html with fragment.only set to TRUE, which means your only creating the <body> part of the html.
  2. it produces a .html.blg file in the path where the .Rmd is located where all syntax highlighting brushes are formatted to work with Yihui's R brush

To use it, simply provide the path to the .Rmd file you want to publish on your blogspot.

rmd2blogger("someRmdFile.Rmd")

Now, you can simply open the .html.blg file in a text editor and copy the complete contents into the html editor for a new post on your blogger site and the code R should look like the one above.

In case you find any bugs, please let me know.

sessionInfo()
## R version 2.15.2 (2012-10-26)
## Platform: x86_64-pc-linux-gnu (64-bit)
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
##  [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=C                 LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] parallel  grid      stats     graphics  grDevices utils     datasets 
## [8] methods   base     
## 
## other attached packages:
## [1] rgdal_0.7-25        raster_1.9-92       gridBase_0.4-6     
## [4] latticeExtra_0.6-19 lattice_0.20-10     RColorBrewer_1.0-5 
## [7] sp_0.9-99           knitr_0.9          
## 
## loaded via a namespace (and not attached):
## [1] digest_0.6.0   evaluate_0.4.3 formatR_0.7    markdown_0.5.3
## [5] plyr_1.7.1     stringr_0.6    tools_2.15.2

To leave a comment for the author, please follow the link and comment on his blog: metvurst.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.