Rchievement of the day #3: Bloggin’ from R

July 1, 2012
By

(This article was first published on ASCIImoose: William K. Morris’s Blog » R, and kindly contributed to R-bloggers)

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(" <", "&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\">", "\\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), titlecategories 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 here or as a gist from:
git://gist.github.com/3027253.git

Related Posts:


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

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.