wordpressR

by Jean-Philippe Fortin

Writing WordPress posts that contain R code becomes a piece of cake with the R packages RWordpress, rmarkdown and knitr. Thanks to John Muschelli who shared his code with me (code inspired by Yihui).

You really need only a few lines of code to transform any R Markdown document into a WordPress post. Look at the RStudio documentation here to learn how to write R Markdown documents.

Assuming that you already have the devtools package installed, make sure to install the following packages as well:

install.packages('RWordPress', repos = 'http://www.omegahat.org/R', type = 'source')
install.packages('knitr')
devtools::install_github("rstudio/rmarkdown")

Here is an example code to publish “my_first_post.Rmd” (located in my directory “~/Dropbox/Wordpress”) under the title “My First Post” (the script can be found on my GitHub):

library(knitr)
library(RWordPress)

# 1. Your WP password:
password <- "myPassword"

# 2. Replace "fortin946"" by your WP username
options(WordPressLogin = c(fortin946 = password))

# 3. Replace "http://jfortinbiostats.com" by your url
options( WordPressURL = 'https://jfortinbiostats.com/xmlrpc.php')

# 4. wpdir must be the directory path of your rmarkdown document
wpdir <- path.expand(file.path("~/Dropbox/Wordpress"))

# 5. Replace "my_first_post.Rmd" by your rmarkdown file
file <- "my_first_post.Rmd" 
rmdname <- file.path(wpdir, file)

# WP will automatically source the images from imgur.com
opts_knit$set(upload.fun = imgur_upload, base.url = NULL) 

# 6. Push your post to WP:
knit2wp(input=rmdname, title="My First post", publish=FALSE) 
Advertisements