I have become a complete knitr addict of late and have been using it in combination with RStudio’s R markdown support on a regular basis. In fact I wrote this post using it! It then dawned on me how great it would be if I could upload the post directly from R/RStudio. It turned out that wasn’t too hard at all. Here’s how.


How to update your Wordpress.com blog from R

Installing the RWordPress package

First I need to install (if I haven't already done so) and load the “RWordPress” package from www.omegahat.org. The “RWordPress” package uses XML-RPC to connect to the Wordpress blogging engine.

if (!require(RWordPress)) {
    install.packages("RWordPress", repos = "http://www.omegahat.org/R")
}
## Loading required package: RWordPress

Connecting to Wordpress.com

Next I set my username, password and website url as R global options.

options(WordpressLogin = c(wkmor1 = "password"), WordpressURL = "http://wkmor1.wordpress.com/xmlrpc.php")

That was not my real password by the way!

The “RWordpress” package provides a bunch of functions (see ?RWordPress) based on methods from the Wordpress and associated APIs.

For example I can use the getUsersBlog function to retrieve metadata about my blogs.

getUsersBlogs()
## Error: need a login and password

Making knitr output compatible with Wordpress.com

If I was hosting a Wordpress based blog myself, then I could go straight to posting knit2html built content straight to my blog. But a Wordpress.com blog is bit more restrictive when it comes to content, so I’ll have to preproccess the html content before I upload it using “RWordPress”.

I have written a little function to extract the body of a knit2html built page and replace the code block markup with Wordpress.com’s shortcode sourcecode blocks. Note this function requires the “XML” package which is available from CRAN.

knit2wp.com <- function(file) {
    require(XML)
    post.content <- readLines(file)
    post.content <- gsub(" <", "&nbsp;<", post.content)
    post.content <- gsub("> ", ">&nbsp;", post.content)
    post.content <- htmlTreeParse(post.content)
    post.content <- paste(capture.output(print(post.content$children$html$children$body, 
        indent = FALSE, tagSeparator = "")), collapse = "\n")
    post.content <- gsub("<?.body>", "", post.content)
    post.content <- gsub("<p>", "<p style=\"text-align: justify;\">", post.content)
    post.content <- gsub("<?pre><code class=\"r\">", "\\[sourcecode language=\"r\"\\]\\\n ", 
        post.content)
    post.content <- gsub("<?pre><code class=\"no-highlight\">", "\\[sourcecode\\]\\\n ", 
        post.content)
    post.content <- gsub("<?/code></pre>", "\\\n\\[/sourcecode\\]", post.content)
    return(post.content)
}

Compiling the R markdown file and posting as blog content

Now it's time to compile this document using knitr. Note that my working directory is set to the directory containing the .Rmd file.

knit2html("Rchievement_of_the_day_3_Bloggin_from_R.Rmd")

Obviuosly I couldn't actually run this code chunk from within the .Rmd file as it would have created a crazy inifinite loop, possibly ending the universe, much like Australia’s new carbon tax.

Now all the that is left to do is publish the post using the “RWordPress” newpost function. The newpost function expects a list of blog parameters which can include the content (named description), title, catergories and tags (named mt_keywords). Setting publish=FALSE uploads the post as a draft.

newPost(
    list(
      description=knit2wp.com('Rchievement_of_the_day_3_Bloggin_from_R.html'),
      title='Rchievement of the day #3: Bloggin&rsquo; from R',
      categories=c('Programming', 'R'),
      mt_keywords=c('rstats', 'blogging', 'XML-RPC', 'R', 'knitr', 'markdown')
    ), 
  publish=FALSE)

Again, this code chunk was set to eval=FALSE

You can get the original .Rmd file of this post as a gist here

Related Posts: