Upload plots as PNG file to your 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.

Synopsis

Note! This post is a addition to Create blog posts from RStudio to WordPress

About a week ago I tried to add my blog to R-Bloggers. I thought everything was correct to add it. But this week I got a mail from Tal Galili (site administrator of R-Bloggers) with the message that my blog uses base64 images in my feed.

This type of images are created, as default option, with knitr as a standalone HTML document. It would be great if I could solve it with PNG instead of base64 images, even better if I could solve it with posting from RStudio to WordPress.

And so I did. In this post I will explain you how to post your WordPress post with PNG files and upload it to your blog.

How to recognize a base64 image

Well you can check your RSS feed of your blog and search for data:image/png;base64, it should be something like this (thx Tal for the image :) ):

Setting your options

To upload your PNG files to your blog you need to set some knitr options first.

As I described in my earlier post you had to set your login parameters to login to your WordPress blog. For the upload of files to WordPress you need to set the upload.fun option of knitr. This will take the filename as the input and returns a link to the image in the RMarkdown file.

Let’s set the login parameters and the upload.fun options. I’ve hashed it out because I can not post to the dummy credentials. In the earlier post you can add the upload.fun option after your login credentials and you are good to go.

# Load libraries
library(RWordPress)
library(knitr)

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

# Upload your plots as png files to your blog
opts_knit$set(upload.fun = function(file){library(RWordPress);uploadFile(file)$url;})

After setting the above credentials your are ready to upload your post with PNG image(s) to your blog. For the completeness I will post a png image in this post I will create within R.

Example of posting your PNG image

data(iris)
pairs(iris, col = iris$Species)

plot of chunk iris

After this I could start post to my blog with knit2wpCrayon.

knit2wpCrayon("r2blog_with_png.Rmd", 
        title = "Upload plots as PNG file to your wordpress",
        categories = c("R", "Programming"), 
        publish = FALSE, upload = TRUE)

Again I hashed it because it could not post itself.

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)