Jeremy Felt

Publishing to WordPress with RStudio

Note: I have no idea what I'm talking about, but it's working, so here we go!

Update July 24, 2020: I changed the XMLRPC library to one that supports httr and added some additional notes at the bottom.

This is an R Markdown document I created using RStudio, an open source IDE for working with R.

One of our clients uses R Markdown as part of their workflow for creating and publishing rich reports with charts and data in HTML. For a while they've been working with the ever so available tools: copy and paste. This seems to work well for the most part, but the images generated as part of the document are included inline and the WordPress editor removes some of that image data during the process of saving after pasting. (I'm pretty sure this is something that used to work, but I haven't gone back and verified yet.)

Our client found a great blog post explaining how to post from R Markdown to WordPress and asked us if that was an option with their site. While I generally have a knee-jerk reaction to publishing with XML-RPC, I don't really have a reason why it shouldn't work. So I checked it out!

Here's the quick version:

  1. Download and install the R binary from one of the many CRAN mirrors.
  2. Download and install RStudio.
  3. Open RStudio.
  4. Create a new R Markdown file.

From what I can tell, there are three important panes in the RStudio window.

  • The file pane used to edit the R Markdown document.
  • The Console pane used to issue commands in R syntax to manipulate the document and its configuration.
  • The Help/Viewer pane on the right displays help information or a preview of the document upon request.

The file pane has an “Knit” menu that provides options for publishing the document to HTML, PDF, or a Word document. Additional configuration is required to “knit” to WordPress.

In the console, I typed these commands:

install.packages( "knitr" )
install.packages( "devtools" )
devtools::install_github(c("josephguillaume/XMLRPC","duncantl/RWordPress"))

This installs the packages required to publish over XML-RPC with commands that WordPress understands.

The josephguillaume/XMLRPC package is a fork of the duncantl/XMLRPC package and uses httr instead of RCurl to make HTTP requests. I believe this is an improvement, though I'm not familiar with the differences.

Once those are setup, an XML-RPC configuration needs to be set. Note the lowercase P in each command.

options(WordpressURL = c("https://yourdomain.com/xmlrpc.php"))
options(WordpressLogin = c( "username" = "password" ))

One minor word of caution: when I find myself publishing via XML-RPC or anything else that requires a plaintext username and password, I add separate user account to my site with a lower set of capabilities and then manually adjust the author information once the post is properly published.

Now we're ready to publish. The RWordPress package provides the knit2wp command. If you type ?knit2wp in the console pane, information appears in the help pane explaining how to manipulate the command to create a new post, edit a post, and assign categories and tags.

To publish this post as a draft on my site, I used:

knit2wp('testpost.Rmd', title='Publishing to WordPress with RStudio', publish = FALSE )

After I published the first version and made some changes, I used the following to edit the post:

knit2wp('testpost.Rmd', title='Publishing to WordPress with RStudio', action=c("editPost"), postid = 13616, publish = FALSE )

And here we are. This is pretty cool!

Caveats

Data URI Images and unfiltered HTML

Note that images generated through RStudio (maybe RMarkdown in general?) are added to content as Data URIs and will not be uploaded to the WordPress media library. This also poses an issue if the user publishing a content is not allowed to push unfiltered HTML. WordPress will strip that data URI as a security precaution when the post content is saved. On single site WordPress, you'll need to be at least an Editor. On multisite WordPress, you'll need to be super administrator.

Ongoing use

If you're familiar with R, this may be obvious, but it wasn't to me! When you start RStudio again after exiting, you can use the library() command to reload required packages:

library('knitr')
library('devtools')
library('RWordPress')
library('XMLRPC')
options(WordpressURL = c("https://yourdomain.com/xmlrpc.php"))
options(WordpressLogin = c( "username" = "password" ))

I'm sure there's a way to maintain a persistent state, but I haven't looked. 🙂

Why R Markdown?

I'm done with the overview, but I'm including this section as an example. The framework for it was provided by RStudio when I first created the document.

From what I can tell, the cars variable is already provided as a dataset. When I type cars in the console pane, I get a list of numbers populating speed and distance columns. If I type summary(cars) in the console, I see the a version of the table listed below, which this markdown document says to embed:

##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

And I can plot that same data into a graph with plot(cars):

plot of chunk Cars plotted

How cool!

Responses and reactions

Mentions

Replies

Cihan replied on 

Thanks for the post, it was easy to follow and implement. Great descriptions.

I think everything worked fine in my test, but I could not get the plot. I also used the built-in cars data set with the default R Markdown template. Any ideas?

    Jeremy Felt replied on 

    The most obvious issue would be if the user account posting the content did not have unfiltered HTML access. On a single WordPress site, this should be editor or administrator. On multisite WordPress, the user would need to be a super admin.

Mike replied on 

I seem to be having some trouble parsing the html to xml. Any suggestions where I might start to debug the messages below?

Editor note: I copy/pasted the code from this comment into a gist for easier reading. JF 12/13/2021

Huanyuan replied on 

Hey I found a small problem in RWordPress

Re: Error: faultCode: 403 faultString: Incorrect username or password.

Because,

We said,

options(WordPressURL = "https://XXXX.com/xmlrpc.php")

However, this URL was not passed to any function under RWordPress correctly on my computer. This is because .server = getServerURL() is not working in any function.

To fix this, I have to manually change .server = getServerURL() to .server = getOption("WordPressURL")

    Jeremy Felt replied on 

    Hi Ram - I'm using RStudio version 1.3.1004 for macOS and R version 4.0.0.

    I'm about to update the post with a reference to a different XMLRPC client that uses httr instead of RCurl. The person I'm working with on this was having trouble connecting via RCurl at all, but httr works fine.

Leave a Reply

Your email address will not be published. Required fields are marked *

The only requirement for your mention to be recognized is a link to this post in your post's content. You can update or delete your post and then re-submit the URL in the form to update or remove your response from this page.

Learn more about Webmentions.