Wednesday, April 30, 2014

Three Ways to Format R Code for Blogger

Unformatted Code

If you are like me originally then you might not think it is worth it to spend the extra energy to format your code.  After all people can just copy what you have and paste it into their preferred editor which will do its own formatting, no sweat.

Well, this might be true for some code in some languages, but in R it really is not gong to fly.  The problem is the ubiquitous <- which is very easily confused with html tags which take the form <tag>. Thus, you are going to be looking for a way to make your code not be buggy when you post it.  One way might be to find and replace all < with &lt; and > with &gt; using a text editor but that is already a fair amount of trouble especially if you actually want to have some HTML tags in your post.  Besides, if you are going to all of that trouble, you might as well as well actually format your posts with syntax highlighting since it will be that much more enjoyable for readers.

Here are three simple ways:

1. Export Code through Notepad++
Exporting through Notepad++ is easy and embeds a css style sheet within the HTML document which makes it easy to edit styles after export.  There is also a fair amount of editing possible before export as well.
  1. Open myscript.R file
  2. Configure appearance of formatting with: Settings>Style>R to your taste
  3. Export HTML: Plugins>NPPExport>Export to HTML
  4. Open saved myscript.html as a text file.
  5. Copy and paste HTML into new post HTML in blogger. 

2. Format with Pretty-R
Pretty-R is useful because it is very simple to use. It also provides links from highlighted function to associated R documentation.  However, the style is fixed and difficult to modify before or after export.
  1. Go to
  2. Paste your R script
  3. Copy HTML code in the HTML box and paste into new post HTML in blogger.

3. Format with knittr
knittr is a pretty amazing package which turns R markdown files into HTML files.  There is a lot of customization possible with it and it is under active development.  I have only recently started looking into it though it seems to me to be the way to go forward. In addition, RStudio supports knittr markdown formatting.  However, it does take a few extra steps to set up but ultimately gives you more control over your final output.
  1. Open RStudio
  2. Select File>New>R Markdown
  3. Paste your code between ```{r} and ``` and structure appropriately.
  4. Specify formatting of your posts as you see fit. There are a lot of great options.  Images will be embedded directly into the HTML document.  This is a pretty amazing feature since it means your file will not require any external dependencies.
  5. Click Knit HTML. This should create an HTML document identical of the same name as the Rmd file.
  6. Edit the generated HTML file.
  7. You may need to remove the ccs classes h1 through h6 as they can interfere with your blogger theme's css classes of the same name.  You may also need to remove the markdown titled identified as <h1> (around line 150-200) if you are going to input your post title in the default blogger title position (which is recommended).
  8. Copy you edited HTML code into blogger

(Note: I know step 7 is a bit hacky so I have asked the question on how to change the default css styles with knitr so that they are not interfering with blogger on stackoverflow.  Please take a look at the question as I hope someone will be able to answer it soon and greatly simply this process.)
Flattr this

No comments:

Post a Comment