Site icon R-bloggers

Create blog posts from RStudio to WordPress

[This article was first published on R – Networkx, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In a couple of courses I’ve used RPubs and GitHub for publishing my R code and work. As I have a own website with WordPress I want to blog from R to my own blog instead only through sharing R code via RPubs and GitHub. After searching over the internet I find some usefull stuff to do this. So let’s try it myself en let this be my first blog post created from Rstudio to WordPress.

Setting up WordPress and R

What do we need for wordpress:
And in R we need:
Load the necessary packages in R

if (!require('RWordPress'))
  install.packages('RWordPress', repos = 'http://www.omegahat.org/R', type = 'source')
library(RWordPress)
library(knitr)

Setting options for RWordPess

options(WordPressLogin=c(your_username="your_password"),
        WordPressURL="http://your.blog.com/xmlrpc.php")

Syntax highlighting with Crayon Syntax Highlighter

To make syntax highlighting work in WordPress with the Crayon Syntax Highlighter plugin. R code should be enclosed in WordPress-shortcode instead of the knitr html output default

<pre><code class="r">...</code></pre>

to

<pre class="lang:r decode:true">...</pre>

We can’t do that with the default knitr2wp function of knitr. knitr2wp is only based on the syntax which is used for SyntaxHighlighter. I don’t want to update all my other code on my blog.

For the Crayon Syntax Highlighter I’ve found a request of allowing proper code highlight when using Crayon. Below the copied requested function for using it to post a blog to my WordPress site with Crayon Syntax Highlighter as code highlighter.

knit2wpCrayon <- function(input, title="A post from knitr", ...,
                          action=c("newPost", "editPost", "newPage"),
                          postid, encoding=getOption("encoding"),
                          upload=FALSE, publish=FALSE, write=TRUE)
{
    out <- knit(input, encoding=encoding)
    on.exit(unlink(out))
    con <- file(out, encoding=encoding)
    on.exit(close(con), add=TRUE)
    content <- knitr:::native_encode(readLines(con, warn=FALSE))
    content <- paste(content, collapse="n")
    content <- markdown::markdownToHTML(text=content, fragment.only=TRUE)
    content <- gsub("<pre><code class="([[:alpha:]]+)">(.+?)</code></pre>",
                    "<pre class="lang:\1 decode:true">\2</pre>",
                    content)
    content=knitr:::native_encode(content, "UTF-8")
    title=knitr:::native_encode(title, "UTF-8")
    if (write){
        writeLines(text=content,
                   con=gsub(x=out, pattern="\.md$", replacement=".html"))
    }
    if (upload){
        action=match.arg(action)
        WPargs=list(content=list(description=content, title=title, 
                                 ...), publish=publish)
        if (action=="editPost") 
            WPargs=c(postid=postid, WPargs)
        do.call("library", list(package="RWordPress", character.only=TRUE))
        print(do.call(action, args=WPargs))
    }
}

After adding the function into R we can post our first blog on WordPress and that would be this one :).

Publishing the post to your blog

Note: Below code should run directly from the R shell (first create the above function and load the RWordPress library) after the R Markdown has created and has runs once. It will post a lots same drafts and ends with a error. I cannot figure out why, I think there is a loop and it wants to post itself to your blog. If you want to upload it run it directly from the R shell with the upload option to TRUE.

knit2wpCrayon("r2blog.Rmd", 
        title = "Create blog posts from RStudio to WordPress",
        categories = c("R", "Programming"), 
        publish = FALSE, upload = TRUE)

## 
## 
## processing file: r2blog.Rmd

## 
  |                                                                       
  |                                                                 |   0%
  |                                                                       
  |.......                                                          |  11%
##   ordinary text without R code
## 
## 
  |                                                                       
  |..............                                                   |  22%
## label: unnamed-chunk-5
## 
  |                                                                       
  |......................                                           |  33%
##   ordinary text without R code
## 
## 
  |                                                                       
  |.............................                                    |  44%
## label: unnamed-chunk-6
## 
  |                                                                       
  |....................................                             |  56%
##   ordinary text without R code
## 
## 
  |                                                                       
  |...........................................                      |  67%
## label: unnamed-chunk-7
## 
  |                                                                       
  |...................................................              |  78%
##   ordinary text without R code
## 
## 
  |                                                                       
  |..........................................................       |  89%
## label: unnamed-chunk-8
## 
  |                                                                       
  |.................................................................| 100%
##   ordinary text without R code

## output file: r2blog.md

[1] "1068" 
attr(,"class") 
[1] "WordpressPostId"

I set the publish option to FALSE, so the post will stay in draft mode and I can fine tune some formats, tags, set a featured image  and add on the top the shortcode for using  syntax.

This code can also be find on my GitHub.

To leave a comment for the author, please follow the link and comment on their blog: R – Networkx.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.