This contribution is part of my ongoing effort of the last few months to collect material for a new book. I want to describe new working procedures in scientific research which are — thanks to progress in digitization like open source tools, open data — now feasible. My focus on these new work flows are guided on the one hand by the goal to improve reproducibility of all research phases and on the other hand to facilitate research procedures in closing the digital gaps between different research tasks. On this page I am going to experiment how to write a program in R and to publish the results directly into WordPress.

1 General remarks

Many of the features are only rudimentary described and implemented. One has to read in detail the description at RWordPress-package. There are listed all the relevant functions with their links to the different WordPress APIs. But most of these links our outdated, because of newer WordPress versions. Now the XML-RPC API is the only relevant code; it supersedes the legacy Blogger, MovableType, and metaWeblog APIs.

A look into the RWordPress-pages in GitHub could also be relevant. See also the XMLRPC package of the same author (Duncan Temple Lang) which is necessary for programming and understanding the functionality of the RWordPress package.

2 Setting startup conditions

2.1 Global chunk options for knitting reports

## Set the global chunk options for knitting reports
knitr::opts_chunk$set(
        echo = TRUE,
        eval = TRUE,
        message = TRUE,
        error = TRUE,
        warning = TRUE,
        highlight = TRUE,
        prompt = FALSE
        )

2.2 Load required packages

We need to load several packages:

  • RWordPress functions as the interface to WordPress
  • ’XMLRPC` is necessary for the transfer protocol
  • knitr as the Swiss knife for dynamic report generation with R.
  • reshape2 for restructuring data sets
  • RCurl as a general Network (HTTP/FTP/…) Client Interface for R
## load required package `RWordPress` and `XMLRPC`
## as the interface and transfer protocol to WordPress
if (!require('RWordPress')) {
        devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
}
Loading required package: RWordPress
library(RWordPress)
library(XMLRPC)
##
## ------------------------------------------------------------------------
##
## load `knitr`: A general-purpose tool for dynamic report generation in R
if (!require("knitr")) {
        install.packages("knitr", repos = 'http://cran.wu.ac.at/')
}
Loading required package: knitr
library(knitr)
##
## ------------------------------------------------------------------------
##
## load `reshape2`: Flexible restructuring and aggregating of data
if (!require("reshape2")) {
        install.packages("reshape2", repos = 'http://cran.wu.ac.at/')
}
Loading required package: reshape2
library(reshape2)
##
## ------------------------------------------------------------------------
##
## load `RCurl`: Provides functions to compose general HTTP requests
if (!require("RCurl")) {
        install.packages("RCurl", repos = 'http://cran.wu.ac.at/')
}
Loading required package: RCurl
Loading required package: bitops
library(RCurl)

2.3 Provide login data and URL for connecting to WordPress

For the connection with WordPress one has to provide log-in data. To prevent that secret data can be seen in the script – or worse – on the public GitHub account I generated variables in my .Rprofile-file with the following code:

options(WordpressLogin = c(yourUserName = 'yourPassword'),
        WordpressURL = 'yourWordPressURL')

The place of the quotes are mandatory. Fill in your own data. The URL is the address for your WordPress installation + xmlrpc.php. For instance https://user.wordpress.com/xmlrpc.php or in my case http://peter.baumgartner.name/xmlrpc.php.

To find the location of your .Rprofile-file read the help section ?Startup. To shorten up the somewhat complicated description: Usually the .Rprofile-file can bee found in the Users’ Home directory. You can locate it with the r_profile() function of the package pathological. The . at the beginning of the file name means that this file normally is not visible. Use the terminal or make your hidden file visible by reading this article.

2.4 Uploade resulting images automatically to imgur.com

In the following chunk I have written commands for uploading automatically all those images (graphics), that are generated by R as output. These pictures are uploaded automatically to the social website imgur.com, a hosting and sharing place for images like flickr or instagram. See for more details to understand the knitr-procedure on Upload images and on the corresponding example page.

opts_knit$set(upload.fun = imgur_upload, base.url = NULL)  # upload all images to imgur.com
opts_chunk$set(fig.width = 5, fig.height = 5, cache = TRUE)

Establishing an upload facility of images is very important because otherwise the transfer to WordPress would encrypt the pictures to an awful peace of code. An example how this looks can be seen from my first trial.