Upload plots as PNG file to your wordpress

February 27, 2016

(This article was first published on R – Networkx, and kindly contributed to R-bloggers)


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

# Login parameters

# 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

pairs(iris, col = iris$Species)

plot of chunk iris

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

        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 on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Mango solutions

RStudio homepage

Zero Inflated Models and Generalized Linear Mixed Models with R

Dommino data lab

Quantide: statistical consulting and training




CRC R books series

Six Sigma Online Training

Contact us if you wish to help support R-bloggers, and place your banner here.

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)