Associate Professor of Sociology at the City University of New York, Queens College

Post to WordPress via R Markdown

Publish to WordPress from RStudio

Blogs are a great communications tool. This post documents a method for generating WordPress posts through RStudio. It uses the RWordPress package by Duncan Temple Lang. This tutorial is basically a highly scaled-down version of  Peter Bumgartner’s excellent tutorial.  At the time of posting, both that and this tutorial are distributed using a CC-BY-NC-SA license (see below).

Set Up Your Session

Installing the RWordPress and XMLRPC Packages

The RWordPress package is being distributed through GitHub. It can be installed using the install_github() operation in the devtools package. You also need to install Lang’s XMLRPC package as well.

library(devtools)
install_github("duncantl/RWordPress")
install_github("duncantl/XMLRPC")

Load Necessary Packages

We need the following packages, in addition to RWordPress and XMLRPC (see above step):

  • knitr, a suite of operations for generating reports
library(RWordPress)
library(XMLRPC)
library(knitr)

Create / Load a Script with Your Credentials

To post to WordPress, your login and password must be included in the script. Posting or circulating a Markdown file with your WordPress login credentials creates a huge web security risk. On the other hand, we teach you to be open about your coding, so that others can review, replicate, and build on it. How to negoatiate the two?

Bumgartner’s solution is to add the following variables to your .Rprofile file using this code

options(WordPressLogin = c(yourUserName = 'yourPassword'),
        WordPressURL = 'yourWordPressURL')

Do not delete the quotation marks in this template. For the ‘yourWordPressURL’, be sure to add a “https://” before and and “/xmlrpc.php” after your site address. So, for the site “joeblog.com”, you would enter “https://joeblog.com/xmlrpc.php

Once you have generated and stored the file, you can add it to any Markdown file that you intend to post. I called my file “WPCrds.R”:

source("E:/Dropbox/Web/WPCrds.R")

Set Images to Post to imgur.com

To save time and resources, post your images to the open online site Imgur.com. To do so, add these knitr options to your script:

# To upload images to imgur.com
opts_knit$set(upload.fun = imgur_upload, base.url = NULL)  

# Set default image dimensions
# Change them to change the dimensions of figures
opts_chunk$set(fig.width = 5, fig.height = 5, cache = TRUE)

Generate a Polished html Report

Create a Markdown file that renders your report in html. Render it locally to something sufficiently polished, and then move on to the next step.

In order to add a picture to this example, let me add a photo:

Old Fourth of July Poster from Library of Congress

Post Your Information

In another chunk, set your post title as the variable postTitle, and define fileName as the name of the Markdown file that you are using to generate the post:

postTitle = "Post to WordPress via R Markdown"
fileName = "Using-RWordPress.Rmd"

Post using the knit2wp command, whose options include:

  • the Markdown file name (option: input)
  • the post title (option: title)
  • whether the post should be published upon rendering (option: publish, set true to directly publish – not advisable)
  • for a new post, the action variable should be set to “newPost” (option: action)
  • to set the post’s category. Use the slugs designated on your site. (option: categories)
  • to set the post’s tags. Use the slugs designated on your site. (option: tags)
postID <- knit2wp(
        input = fileName, 
        title = postTitle, 
        publish = FALSE,
        action = "newPost",
        categories = c("data-analytics"),
        tags = c("markdown"),
        mt_excerpt = "Publish a Markdown report direct to your WordPress blog",
)

Clean It Up on WordPress

From here, you will have a draft post on your WordPress site.  I find WordPress to have a far more user-friendly interface to complete the last steps of a post.

Remember: Risk the temptation to do everything in R if it costs a lot of time.  Yes, you would be cool if you could do it.  The problem is no one but data nerds will appreciate it, and there’s not a lot of data nerds out there.

Summary

To summarize, start with this set up to your script. Remember that the source() line points to a file with your credientials:

library(RWordPress)
library(XMLRPC)
library(knitr)

opts_knit$set(upload.fun = imgur_upload, base.url = NULL)  
opts_chunk$set(fig.width = 5, fig.height = 5, cache = TRUE)

source("E:/Dropbox/Web/WPCrds.R")

postTitle = "Your Title"
fileName = "Your-Markdown-File.Rmd"

Then write the script until it is polished, and then you can post by running this code. I recommend that you set eval = F, so that it doesn’t run when you render the Markdown file into html. Then, once the file runs, execute this code on its own, not as part of a larger operation.

postID <- knit2wp(
        input = fileName, 
        title = postTitle, 
        publish = FALSE,
        action = "newPost",
        categoories = c("Insert Slugs here"),
        tags = c("insert slugs here"),
        mt_excerpt = "Insert excerpt here"
)

Then, polish the piece in WordPress.

Leave a Reply

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