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.
Hello STEPHEN,
ReplyDeleteThis 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)
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.
ReplyDeleteHi Neil, just one point -
ReplyDeleteThe 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...
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.)
ReplyDeletehttp://blog.dexy.it/241
Super helpful, thanks!
ReplyDeleteThanks very much for this helpful post!
ReplyDeleteIn case anyone else is a novice and stumbles for a few minutes wondering why the gist is not highlighting properly when you select from the language drop down. The reason is, you should name the file with the file extension you want it to highlight. In my case 'Mysnippet.r'.