Posting on WordPress from R: A Few Small Hurdles

27 Dec, 2013 — 2 min

In case it might help someone (or more likely, when I forget what I learned) here are a few things I picked up as I did the post below. I think it’s pretty cool that you can do this, and props to Yihui Xie, the author of knitr, and RStudio who have made R incredibly better.

As Yihui documented in this post, the first step is to download the RWordPress package from Omegahat. If you are on Windows, you have to build RWordPress. I had studiously avoided building anything in R but finally couldn’t dodge it any more. To do this you have to install RTools. Lesson learned with Rtools: make sure the path variable is set correctly. The next step is to run this code:

library(RWordPress)
options(WordpressLogin = c('myusername' = 'mypassword'),
WordpressURL = 'http://www.mywebsite.com/xmlrpc.php')
library(knitr)
knit2wp('yourfile.Rmd', title = 'Your post title')

It worked the first time… when has that ever happened? The only problem was that WordPress doesn’t run JavaScript and the googleVis table didn’t show up (it needs js). A trick that’s out there is to use a custom field in WordPress as described here but it didn’t work in this case.

I ended up using an iframe as suggested here which got the job done. You have to save the code from R as a HTML file (code:

print(InvestmentTable, "chart", file="FaberGoogleTable.html")

and upload it to WordPress, then add it in an iframe. Not the easiest solution, but gets the job done.