This post has been written in Rstudio using RMarkdown and then uploaded to wordpress.com 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
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
:
install.packages('RWordPress', repos = 'http://www.omegahat.org/R', type = 'source')
On CentOS the required package is libxml2-devel, however I did not manage to get R to post to wordpress.com on this platform (several HTML errors)
Publishing to wordpress.com
There are four steps to publish to wordpress.com.
1. If you have two-stage verification set up then you have to login to wordpress.com 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
library(RWordPress) options(WordPressLogin = c(user = 'password'), WordPressURL = 'https://user.wordpress.com/xmlrpc.php')
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 wordpress.com. The second argument then is https://my_username.wordpress.com/xmlrpc.php
. 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
library(knitr) opts_knit$set(base.url = 'https://dl.dropboxusercontent.com/u/3038814/wpintro/', 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(x,y)
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
setwd('~/Dropbox/Public/wpintro') knit2wp('/path/to/RMD/file', title = 'My Post Title', publish = FALSE, shortcode = TRUE)
The publish=FALSE
flag allows you to first review the post on WordPress.com before publishing it.