Publishing from R straight to

This post has been written in Rstudio using RMarkdown and then uploaded to using the knitr package of Yihui. The way in which this is done is excellently documented here and here. In this post I just wish to outline some minor observations and difficulties I encountered.


Installation of RWordPress, the required package for posting from R to WordPress, was pretty straightforward after installing the xml2-dev package on Ubuntu 14.04 using sudo apt-get install libxml2-dev:

                  repos = '',
                  type = 'source')

On CentOS the required package is libxml2-devel, however I did not manage to get R to post to on this platform (several HTML errors)

Publishing to

There are four steps to publish to

1. If you have two-stage verification set up then you have to login to and set an Application Password. All this does is generate a code which can be used by R or any other application for that matter, to bypass the two-stage authentication. I suggest you do this everytime you need to do a post and then delete the application password after each post.

2. Set your login options. This is done through

options(WordPressLogin = c(user = 'password'),
        WordPressURL = '')

One note about this, the label user is your actually username. So say your username is my_username then the function argument would be c(my_username = 'password'). The password is enclosed within the inverted commas and, if you have two-stage authentication, this would be the application password generated by The second argument then is Also please note that https needs to be used and not http.

3. Set the image base directory. Here I will follow Yihui’s recommendation and suggest the use of a Dropbox public folder for this, for example ~/Dropbox/Public/wpintro. The options are then set as follows

opts_knit$set(base.url = '',
              base.dir = '~/Dropbox/Public/wpintro/')

where 3038814 is replaced by your own user code. This can be found by simply uploading a file into the public folder, going on the Dropbox website, right clicking on the file and choosing Copy public link….

I show this works in principle by plotting the following figure:

x <- seq(0,100,by=0.1)
y <- sin(2*pi*0.1*x)

plot of chunk unnamed-chunk-4

4. I had a small problem with paths which I haven’t resolved yet. In particular, I only manage to get the figures in the right subdirectory if I change the path to ~/Dropbox/Public/wpintro/ and run knitr from this location. This is not much of a problem, so I will keep it at that for now. The final commands you need to run therefore are

        title = 'My Post Title',
        publish = FALSE,
        shortcode = TRUE)

The publish=FALSE flag allows you to first review the post on before publishing it.