WordPress posts with R Markdown

It seems appropriate for my first post to detail how this very post made it here. Most of the posts I’m planning will have at least some connection to data, and those will rely often on R code. So in the spirit of reproducibility, that code will be included in the posts themselves, as embedded code chunks. Such reproducibility is perhaps most easily accomplished using dynamic documents, and specifically the R version of markdown. Below are a set of code chunks that, taken together, duplicate the contents of an R document that I used to post this post to WordPress.com directly from the R environment.

Each code chunk begins with 3 back ticks and options to (in this case) display the code (echo=TRUE) but not evaluate it (eval=FALSE). The first step is to install the RWordPress package from source, and then make it available.

install.packages('RWordPress', repos = 'http://www.omegahat.org/R', type = 'source')

Next, set some global options to inform WordPress of your credentials and blog url. Obviously I changed what you see below in my own version. I also added the table of contents (“toc”) option to the default options in the HTML renderer in the markdown package. I won’t use a toc in this post, but plan to in later posts.

options(WordPressLogin = c(your_username = 'your_password'),
WordpressURL = 'http://your_wordpress_blog.com/xmlrpc.php')

# include a table of contents
options(markdown.HTML.options = c(markdownHTMLOptions(default = T),'toc'))

Now I’m ready to call the knit2wp function–a wrapper function around the RWordPress package that compiles an R Markdown document to HTML and posts it to WordPress.


# Usage:
# knit2wp(input, title = 'A post from knitr', ..., shortcode = FALSE,
# action = c('newPost', 'editPost', 'newPage'), postid,
# encoding = getOption('encoding'), publish = TRUE)

title = 'test post 1',
action = 'newPost',
mt_keywords = c('R', 'knitr', 'knit2wp', 'RWordPress', 'WordPress', 'blog'),

I plan to keep each unique call to knit2wp (new posts or edits to existing posts) in sequential order in my aformentioned R document. This way I have a record of the parameters I used in each subsequent post.

For more information about using RMarkdown to create WordPress posts see: