---
title: "Blog Posting from R Markdown to WordPress"
author: "Heather Urry (@HeatherUrry)"
date: "August 12, 2017"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, eval = FALSE)
```
## Smart People

Whoa. Some people are really friggin' smart. Well, lots of people are, but right now I'm specifically referring to Duncan Temple Lang ([@duncantl](http://twitter.com/duncantl)), who developed the R package, **RWordPress**, and William K. Morris ([@wkmor1](http://twitter.com/wkmor1)), Yihui Xie ([@xieyihui](http://twitter.com/xieyihui)), and Jared Lander ([@jaredlander](http://twitter.com/jaredlander)), who developed the knit2wp function in **knitr**. These two packages make it possible to write a blog post in R Markdown and then publish it directly to a WordPress site. This post walks you through the steps to do so, inspired in large part by [Xie's instructions](https://yihui.name/knitr/demo/wordpress). 

[cue sparklers] 

<img src="https://mfr.osf.io/export?url=https://osf.io/rwspb/?action=download%26direct%26mode=render&initialWidth=765&childId=mfrIframe&format=1200x1200.jpeg" width="60%" height="60%" align=CENTER alt="Photo by JD Weiher on Unsplash at https://unsplash.com/photos/AHo10GK0vLY"/>

## Three Easy Steps

### 1. Install the relevant packages
First, you need to install the relevant packages, which are **knitr**, **devtools**, and **RWordPress**. Here's the R code:

```{r install}

if (!require('knitr')) {
  install.packages("knitr")
}
if (!require('devtools')) {
  install.packages("devtools")
}
if (!require('RWordPress')) {
  devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
}

```
### 2. Write Your Blog Post in R Markdown

Now write your blog post in R Markdown. You can use this very post as a starting point, if you'd like. I've linked the Rmd file [here](https://mfr.osf.io/render?url=https://osf.io/hj2yq/?action=download%26mode=render), which I created in RStudio (File --> New File --> R Markdown...). The Rmd file ultimately contains all of your blog post content. You can find all kinds of help with R Markdown [by simply Googling it](http://lmgtfy.com/?q=R+Markdown+basics).

### 3. Upload

Once you've created your blog post as an Rmd file, it's time to publish it. You first need to tell RWordPress the information to use to get to your WordPress site. Simply input the correct user, password, and URL in the relevant places in the options line below.

```{r publish}

#activate the necessary libraries
library(RWordPress)
library(knitr)

# Tell RWordPress how to set the user name, password, and URL for your WordPress site.
options(WordpressLogin = c(user = 'password'),
        WordpressURL = 'https://yoursite.wordpress.com/xmlrpc.php')
```

Finally, invoke the knit2wp function, which tells knitr to create the html code and upload it to your WordPress site. Specify the Rmd file name, blog post title, and any additional arguments. Here I've set a "publish = FALSE" argument; this means that the post will get uploaded but it won't be publicly available just yet. This is ideal if you want to head to your WordPress site to preview the post before publishing manually. If you set "publish = TRUE", then it'll be publicly available immediately.

```{r knit2wpquery}

# Optional: If you want to see all of the knit2wp arguments that are possible, run this line.
?knit2wp

# If need be, set your working directory to the location where you stored the Rmd file. 
setwd("C:/Users/user/Documents")

# Tell knitr to create the html code and upload it to your WordPress site
knit2wp('RWordPress_post.Rmd', title = 'Blog Posting from R Markdown to WordPress',publish = FALSE)

```

## Done
That's it. I'm pretty new to doing this but I already love being able to write blog posts in R Markdown. It's perfectly suited to the task, even for posts that don't rely on displaying and/or running R code. The **RWordPress** and **knitr** packages in combination with R Markdown make it especially easy for WordPress bloggers to get their ideas out of the brain and into public view. 

It's also worth pointing out something I learned from Kris Shaffer ([@krisshaffer](http://twitter.com/krisshaffer)), when he appeared on a [Teaching in Higher Ed podcast recently](http://teachinginhighered.com/podcast/what-we-should-know-about-apis/). https://twitter.com/bonni208/status/888460349493719041 He made the good point that one should retain personal copies of one's you're using. When you have your blog post saved in .Rmd format, you've achieved this goal. Bonus. 

<iframe src="https://giphy.com/embed/nXxOjZrbnbRxS" width="50%" height="50%" frameBorder="0" class="giphy-embed" allowFullScreen></iframe>

Have you posted to WordPress through R Markdown? Got any general suggestions for blogging via R Markdown? Share your experiences in the comments below or via Twitter [@HeatherUrry](http://twitter.com/HeatherUrry).