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 getUsersBlogs function to retrieve metadata about my blogs.
getUsersBlogs()
## $isAdmin ## [1] TRUE ## ## $url ## [1] "http://wkmor1.wordpress.com/" ## ## $blogid ## [1] "25365214" ## ## $blogName ## [1] "William K. Morris’s Blog" ## ## $xmlrpc ## [1] "http://wkmor1.wordpress.com/xmlrpc.php" ##
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(" <", " <", post.content)
post.content <- gsub("> ", "> ", 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\">", "\\1\\\n ",
post.content)
post.content <- gsub("<?pre><code class=\"no-highlight\">", "\\1\\\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")
Obviously 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 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, categories 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’ 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 here or as a gist from:
git://gist.github.com/3027253.git
Related Posts:
- Using knitr and RWordPress to publish results directly from R (Carl Boettiger: Theoretical Ecology and Evolution)
- How to Convert Sweave LaTeX to knitr R Markdown: Winter Olympic Medals Example (Jeromy Anglim‘s Blog: Psychology and Statistics)
- Making Reproducible Research Enjoyable (Statistics, R, Graphics and Fun)

Thanks for the post, I will try soon.
Pleasure
Pingback: How to post R code on WordPress blogs | Pairach Piboonrungroj
I’m really glad to see more and more people posting the source code of their blog posts as github gists. Great job!
Thanks Yihui. And I am glad that you made us a tool as useful as knitr!
Pingback: An R-chitecture for Reproducible Research/Reporting/Data Journalism « OUseful.Info, the blog…
Cool, this is exactly the workflow I’ve been looking for for publishing from rmarkdown to wordpress. I also think an RPubs embed tag for wordpress would be cool, similar to the ones for tweets, etc.
[rpub]http://rpubs.com/LanceBraswell/example[/rpub] or osmething.
Pingback: Local News Templates – A Business Opportunity for Data Journalists? « OUseful.Info, the blog…
Pingback: Work with Rstudio and WordPress blogs | My Happy PhD Life
Can you comment a little about the differences between hosting your own wordpress site and wordpress.com? I can use knit2html and post directly, but the syntaxhighlighter is not resolving quotes and < correctly. I tried using your knit2wp.com function and only receive a Error: faultCode: -32700 faultString: parse error. not well formed.
Yihui has made some improvements to the knit2wp function. Maybe try giving that a go. http://yihui.name/knitr/demo/wordpress/
Thanks for the post.
By the way I think the correst spelling for the option is WordPressLogin, at least with my RWordPress version.
No worries. Good point. I think wordpress.com is somehow forcing the P to be uppercase. In my sourcecode (see the git repos) I have it the correct way.