Rchievement of the day #3: Bloggin’ from R

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:

This entry was posted in Programming, R and tagged , , , , , . Bookmark the permalink.

19 Responses to Rchievement of the day #3: Bloggin’ from R

  1. Pairach says:

    Thanks for the post, I will try soon.

  2. Pingback: How to post R code on WordPress blogs | Pairach Piboonrungroj

  3. Yihui says:

    I’m really glad to see more and more people posting the source code of their blog posts as github gists. Great job!

  4. Pingback: An R-chitecture for Reproducible Research/Reporting/Data Journalism « OUseful.Info, the blog…

  5. Andrew says:

    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.

  6. Pingback: Local News Templates – A Business Opportunity for Data Journalists? « OUseful.Info, the blog…

  7. Pingback: Work with Rstudio and WordPress blogs | My Happy PhD Life

  8. norcalbiostat says:

    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.

  9. karlforner says:

    Thanks for the post.
    By the way I think the correst spelling for the option is WordPressLogin, at least with my RWordPress version.

    • wkmor1 says:

      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.

  10. Hi, RWordPress looks amazing. But really, I need help. I also apologize if this takes to much of the space.

    FYI, unfortunately, I could not ge it installed on my windows with R 3.0.1 (R Studio). I typed ‘install.packages(“RWordPress”, repos = “http://www.omegahat.org/R”,type=”source”)’.

    I got these:
    ERROR: dependency ‘bitops’ is not available for package ‘RCurl’
    * removing ‘D:/My Documents – KIM/R/win-library/3.0/RCurl’
    Warning in install.packages :
    running command ‘”C:/PROGRA~1/R/R-30~1.1/bin/x64/R” CMD INSTALL -l “D:\My Documents – KIM\R\win-library\3.0″ C:\Users\kim\AppData\Local\Temp\RtmpE91Atf/downloaded_packages/RCurl_1.95-4.tar.gz’ had status 1
    Warning in install.packages :
    installation of package ‘RCurl’ had non-zero exit status
    ERROR: dependency ‘RCurl’ is not available for package ‘XMLRPC’
    * removing ‘D:/My Documents – KIM/R/win-library/3.0/XMLRPC’
    Warning in install.packages :
    running command ‘”C:/PROGRA~1/R/R-30~1.1/bin/x64/R” CMD INSTALL -l “D:\My Documents – KIM\R\win-library\3.0″ C:\Users\kim\AppData\Local\Temp\RtmpE91Atf/downloaded_packages/XMLRPC_0.3-0.tar.gz’ had status 1
    Warning in install.packages :
    installation of package ‘XMLRPC’ had non-zero exit status
    ERROR: dependency ‘XMLRPC’ is not available for package ‘RWordPress’
    * removing ‘D:/My Documents – KIM/R/win-library/3.0/RWordPress’
    Warning in install.packages :
    running command ‘”C:/PROGRA~1/R/R-30~1.1/bin/x64/R” CMD INSTALL -l “D:\My Documents – KIM\R\win-library\3.0″ C:\Users\kim\AppData\Local\Temp\RtmpE91Atf/downloaded_packages/RWordPress_0.2-3.tar.gz’ had status 1
    Warning in install.packages :
    installation of package ‘RWordPress’ had non-zero exit status

    Really appreciate if you guys could help shed some lights …

  11. Pingback: Publishing R markdown files as blog post | Technology & Programming

  12. Pingback: 24 Days of R: Day 1 | PirateGrunt

  13. Pingback: Blogging from RStudio | Hawthorne Effect

  14. Pingback: 24 Days of R: Day 1 | rmsharpdotme

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s