Wednesday, 6 April 2016

A workflow for publishing RStudio notebooks on Blogger 6.4.16

The past few years, I have been searching regularly for ways of formatting R code on +Blogger. Although different possibilities were available, almost always I ended up using the online Pretty R syntax highlighter by copying parts of a script and pasting these in blog posts after highlighting.

Thanks to the excellent work of all people at +RStudio however, we can now easily incorporate markdown content in our R scripts (by compiling them as notebooks), which seems to be very much suited for writing R-related blog posts. While creating nicely formatted HTML documents is straightforward now, the problem moves to that of getting an HTML document published as a Blogger post without destroying the formatting... Which is not straightforward if you're not an HTML/JavaScript/CSS expert.

Luckily I found a workaround, which I'm outlining below, for my own future reference! :)

Create your R script

As an example, I'm using my last post on variography with gstat and ggplot2. You can compile the notebook into a self-contained HTML document, but the images are then included as Base64 code, and do not get picked up when sharing the post url on e.g. social networks. Therefore, I chose not to have the HTML self-contained at this point, which means the images have to be uploaded to Blogger at some point, or hosted somewhere else with modifications of the image urls in the html code. The original script can be found here.

Compile your notebook

Once your script is complete, press the "Compile Notebook" button, or use Ctrl+Shift+K to compile your script to an RStudio notebook. Once the RStudio HTML preview pops up, check if everything is there.

Get self-contained html body code (without images)

Now select the contents of the preview, copy it, and paste it into the Blogger Compose editor. Everything should look exactly as in the RStudio HTML preview, except for the images, which should not be there at this point.

Modify image urls

For inserting the images, switch to the HTML editor, and search for the "img" HTML tags. Then modify the src attributes, so they point to the urls of the images (you can host them wherever you want, but I'm using Google Drive). Another way of doing this is to upload the images in the Compose editor, and insert them in the correct place within the notebook.

Further modify HTML if necessary

Now save and preview your post. Are things still looking as expected? Then you can probably skip this step. If some weird things happened to your notebook, you might want to check for incompatibilities with your blog CSS code. In my case, I had to set border-left to 0px for the code blocks, as my blog CSS uses a border there. This was quickly solved by doing a find and replace for which I used this R function on a text file with HTML code:

find_and_replace('html.txt','style="','style="border-left: 0px;')
After modifying the HTML code, insert it again in the Blogger HTML editor.


Finally publish the post, and enjoy the RStudio notebook that just appeared on your blog!

Any people out there doing similar things? Please let me know!
5 Bart Rogiers: A workflow for publishing RStudio notebooks on Blogger The past few years, I have been searching regularly for ways of formatting R code on +Blogger . Although different possibilities were avail...


  1. This is a pretty cool approach, but it's still annoying to have to write into code blocks. I keep looking for the approach where all comments are converted to normal text, and all R code is wrapped in `pre` and `code` tags.

    1. You don't have to modify the HTML if there are no conflicts with the Blogger template you are using! Just try and see if it works for you without this step. You can of course also modify the template to remove all conflicts. If you then do use the self-contained HTML, it's just copy and paste!

  2. How did you format your code's syntax to look nice? I am trying to play around with the different highlight themes, and it does not seem like it is working for me. I think I'm having a hard time making changes, and making sure those changes are re-compiled to ensure they are in effect.

    1. Hi Peter, I'm not sure what exact your problem is: Are you having difficulties in getting the code highlighting in the RStudio HTML preview like the one I show here? Or does the highlighting disappear when you transfer everything to Blogger? If it is the latter you're struggling with, then I would first check if everything looks fine in the Blogger Compose editor. And if it does, you'll probably need to spend some time on your blog's CSS code ...


< >