Rchievement of the day #3: Bloggin’ from R

[This article was first published on ASCIImoose: William K. Morris’s Blog » R, 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.

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.


 ## $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) {
    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 <- gsub("<?pre><code class=\"no-highlight\">", "\\1\\\n ",
    post.content <- gsub("<?/code>
", "\\\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.


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), titlecategories and tags (named mt_keywords). Setting publish=FALSE uploads the post as a draft.

      title='Rchievement of the day #3: Bloggin’ from R',
      categories=c('Programming', 'R'),
      mt_keywords=c('rstats', 'blogging', 'XML-RPC', 'R', 'knitr', 'markdown')

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:

Related Posts:

To leave a comment for the author, please follow the link and comment on their blog: ASCIImoose: William K. Morris’s Blog » R.

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.

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)