Note: The described setup was last tested on 02/2013 and may require changes to work with current versions of WordPress
For blogging from R to WP, I recommend:
RWordPress
R package for uploading posts from R to WPYou will need to build RWordPress
yourself, even on Windows. RWordPress
depends on the packages RCurl
, XML
, and XMLRPC
. Note that XMLRPC
is available from the BioConductor repository. On Linux, all build tools should already be installed, on Windows, download Rtools<version>.exe
and follow the instructions from Building R for Windows. Then build and install RWordPress
:
install.packages("RWordPress", repos="http://www.omegahat.org/R", build=TRUE)
Set up RWordPress
with your login credentials and the site URL.
library(RWordPress)
options(WordpressLogin=c(user="password"),
WordpressURL="http://your_wp_installation.org/xmlrpc.php")
To make syntax highlighting work in WP with the SyntaxHighlighter plugin, R code should be enclosed in WP-shortcode instead of the knitr html output default <pre><code class="r">...</code></pre>
like so:
[code lang='r']
...
[/code]
One option is to set up knitr itself to wrap code into WP-shortcode format. The downside to this option is that the output html is only usable within WP, but not as a standalone html page. Adapted from Carl Boettiger:
$set(output=function(x, options) paste("\\[code\\]\n", x, "\\[/code\\]\n", sep=""))
knit_hooks$set(source=function(x, options) paste("\\[code lang='r'\\]\n", x, "\\[/code\\]\n", sep="")) knit_hooks
As an alternative, you can use the XML
package to extract the html body produced by knitr and clean it to make it work for WordPress. Adapted with small modifications from William K. Morris:
function(file) {
knit2wp <-require(XML)
readLines(file)
content <- htmlTreeParse(content, trim=FALSE)
content <-
## WP will add the h1 header later based on the title, so delete here
$children$html$children$body$children$h1 <- NULL
content paste(capture.output(print(content$children$html$children$body,
content <-indent=FALSE, tagSeparator="")),
collapse="\n")
gsub("<?.body>", "", content) # remove body tag
content <-
## enclose code snippets in SyntaxHighlighter format
gsub("<?pre><code class=\"r\">", "\\[code lang='r'\\]\\\n",
content <-
content) gsub("<?pre><code class=\"no-highlight\">", "\\[code\\]\\\n",
content <-
content) gsub("<?pre><code>", "\\[code\\]\\\n", content)
content <- gsub("<?/code></pre>", "\\[/code\\]\\\n", content)
content <-return(content)
}
In WP, you have to enable the “XML-RPC” option in Settings -> Writing -> Remote Publishing.
In R, first set the working directory to the one containing the html page. Then use newPost(..., publish=FALSE)
to stage the post in WP as a draft to actually publish later from the dashboard. This uses knit2wp()
as defined above:
newPost(content=list(description=knit2wp('rerWorkflow.html'),
title='Workflow: Post R markdown to WordPress',
categories=c('R')),
publish=FALSE)
If you plan to edit the post later and upload the changed html, save the return value from newPost()
: It is the post id, necessary to identify the post using editPost()
.
99 # post id returned by newPost()
postID <-editPost(postID,
content=list(description=knit2wp('rerWorkflow.html'),
title='Workflow: Post R markdown to WordPress',
categories=c('R')),
publish=FALSE)
For me, all this works fine, but I need to make sure the draft is opened first with the WP html editor, not the visual editor. So the html editor has to be “active”, i.e., was used last. After openening the draft with the html editor, I have to switch to the visual editor, and then hit “publish”. Publishing the post while still in the html editor does not work. Further switching between visual and html editor messes everything up.
knit2wp()
functionknitr now provides it’s own knit2wp()
function that is similar to the one defined above. In addition, it also wraps the call to newPost()
: see the knitr to WP example.
R markdown - markdown - R code - all posts