Inauguration post

I will start this blog by writing about how I started this blog. One of the first things I wanted to make sure was to be able to blog from R, or RStudio and not have to use the WordPress.com editor. So using RStudio this is actually pretty simple and I can take it easy with my first post and reblog what others have invented to make it happen.

I write my blog entries in RStudio using Rmarkdown and then push them to the WordPress servers. And the way I implemented this is much along the lines of what is described here.

Install R package and link to WordPress

Hence, first install the RWordPress package

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

and link it to your account (insert your username for ‘yourWPusername’ and your password for ‘yourWPpassword’)

options(WordPressLogin = c(yourWPusername = "yourWPpassword"), WordPressURL = "https://yourWPusername.wordpress.com/xmlrpc.php")

You can do a quick whether your credentials are working by calling

getUsersBlogs()

So far so good.

Handling graphs and figures

When you blog about R, I guess its reasonable to expect to produce many graphs to show on your blog. I handle graphs following Yihui Xie’s suggestion and save them in my dropbox. I created a separate folder in my Public folder called wp that is a bucket for all the figures that I create. Then I tell knitr that I want to save any figure created in this folder.

opts_knit$set(base.url = 'https://dl.dropboxusercontent.com/u/some_number/wp/',
              base.dir = 'C:/<path to dropbox>/Dropbox/Public/wp/')

The first line includes the URL of the wp folder which you can find by putting a little file in it and right-clicking on it to copy the link. Then insert the copied link and the path to your newly created folder into the snippet above.

knit2html -> WordPress.com format

Suppose your have your blog post saved in a file post.Rmd in the Rmarkdown .Rmd format. Using the function knit2htmlyou can turn it into an HTML file (in RStudio simply press the the ‘Knit HTML’ button at the top). To push the content of the HTML file to WordPress one has to extract the body of the HTML file, otherwise WordPress.com won’t accept it (at least when you are hosting your blog with WordPress.com – which I do).

On the blog I linked above, the author provides a function to sanitize the HTML file and return a string that you can pass on to WordPress using the RWordPress function newPost. But for me it didn’t work, because it inserted crazy special characters here and there. After googling a bit, I found an alternative solution (I forgot where, feel free to point me to the original and I will happily link to it):

knit2wp <- function(file) {
    require(XML)
    content <- readLines(file)
    content <- htmlTreeParse(content, trim=FALSE)

    ## WP will add the h1 header later based on the title, so delete here
    content$children$html$children$body$children$h1 <- NULL
    content <- paste(capture.output(print(content$children$html$children$body,
                                          indent=FALSE, tagSeparator="")),
                     collapse="\n")
    content <- gsub("<?.body>", "", content)         # remove body tag

    ## enclose code snippets in SyntaxHighlighter format
    content <- gsub("<?pre><code class=\"r\">", "\\\\\n",
                    content)
    content <- gsub("<?pre><code class=\"no-highlight\">", "\\\\\n",
                    content)
    content <- gsub("<?pre><code>", "\\\\\n", content)
    content <- gsub("<?/code></pre>", "\\[/code\\]\\\n", content)
    return(content)
}

Workflow

The way it now works for me is as follows. Suppose I have my post written in Rmarkdown and saved in the file test.Rmd. Set the current working directory to the location of this file and then run the following script:

library(RWordPress)
options(WordPressLogin = c(yourWPusername = "yourWPpassword"), WordPressURL = "https://yourWPusername.wordpress.com/xmlrpc.php")
library(knitr)
opts_knit$set(base.url = 'https://dl.dropboxusercontent.com/u/some_number/wp/',
              base.dir = 'C:/<path to dropbox>/Dropbox/Public/wp/')

knit2wp <- function(file) {
    require(XML)
    content <- readLines(file)
    content <- htmlTreeParse(content, trim=FALSE)

    ## WP will add the h1 header later based on the title, so delete here
    content$children$html$children$body$children$h1 <- NULL
    content <- paste(capture.output(print(content$children$html$children$body,
                                          indent=FALSE, tagSeparator="")),
                     collapse="\n")
    content <- gsub("<?.body>", "", content)         # remove body tag

    ## enclose code snippets in SyntaxHighlighter format
    content <- gsub("<?pre><code class=\"r\">", "\\\\\n",
                    content)
    content <- gsub("<?pre><code class=\"no-highlight\">", "\\\\\n",
                    content)
    content <- gsub("<?pre><code>", "\\\\\n", content)
    content <- gsub("<?/code></pre>", "\\[/code\\]\\\n", content)
    return(content)
}

knit2html("test.Rmd")

newPost(
    list(
        description=knit2wp('test.html'),
        title='Test',
        categories=c('Programming', 'R'),
        mt_keywords=c('rstats', 'blogging', 'XML-RPC', 'R', 'knitr', 'markdown')
    ),
    publish=FALSE)

Et voilá!

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

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