Tuesday, September 7, 2010

Embed R Code with Syntax Highlighting on your Blog

Note 2010-11-17: there's more than one way to do this. See the updated post from 2010-11-17.

If you use blogger or even wordpress you've probably found that it's complicated to post code snippets with spacing preserved and syntax highlighting (especially for R code). I've discovered a few workarounds that involve hacking the blogger HTML template and linking to someone else's javascript templates, but it isn't pretty and I'm relying on someone else to perpetually host and maintain the necessary javascript. Github Gists make this really easy. Github is a source code hosting and collaborative/social coding website, and gist.github.com makes it very easy to post, share, and embed code snippets with syntax highlighting for almost any language you can think of.

Here's an example of some R code I posted a few weeks ago on making QQ plots of p-values using R base graphics.



The Perl highlighter also works well. Here's some code I posted recently to help clean up PLINK output:



Simply head over to gist.github.com and paste in your code, select a language for syntax highlighting, and hit "Create Public Gist." The embed button will give you a line of HTML that you can paste into your blog to embed the code directly.

Finally, if you're using Wordpress you can get the Github Gist plugin for Wordpress to get things done even faster. A big tip of the had to economist J.D. Long (blogger at Cerebral Mastication) for pointing this out to me.

5 comments:

  1. Hello STEPHEN,

    This is cool for blogger, I am not sure if it will work with wordpress.com (since they don't allow the insertion of javascript code)

    If you are using a self hosted wordpress (wordpress.org), then there is also this solution:
    http://www.r-statistics.com/2010/02/r-syntax-highlight-on-your-blog-a-wordpress-plugin/

    (although JD solution is cool - thanks for sharing)

    ReplyDelete
  2. Yes, Wordpress.com is very annoying: no R syntax in the highlighter that they provide and no possibility to embed gists. And in my experience, no likelihood of a positive response to requests. For Wordpress users, self-hosted is the way to go if you can't live with this.

    ReplyDelete
  3. Hi Neil, just one point -
    The WordPress team WILL implement R syntax highlighting, but only if it will be provided according to this brush syntax:

    http://alexgorbatchev.com/SyntaxHighlighter/manual/brushes/custom.html

    Since this is what they use (http://en.support.wordpress.com/code/posting-source-code/).

    I was thinking that maybe the code yihui wrote for creating a npptoR code (http://yihui.name/en/wp-content/uploads/2010/08/Npp_R_Auto_Completion.r)

    Might be manipulated to create this brush, but didn't get around to doing that.

    I'll post about it in the R mailing list and see if someone feels like helping...

    ReplyDelete
  4. I've written some open source document automation software called Dexy which was designed with R very much in mind. You can use it to highlight your source code offline, incorporate this into your blog post and then upload to WordPress via the API. You just need to be able to add a stylesheet or modify the CSS of your blog's template, if you can do that it should work for wordpress.com or self-hosted wordpress. (I'll be looking into adding Blogger support soon too.)

    http://blog.dexy.it/241

    ReplyDelete

Creative Commons License
Getting Genetics Done by Stephen Turner is licensed under a Creative Commons Attribution-NonCommercial 3.0 Unported License.