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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define the function | |
ggd.qqplot = function(pvector, main=NULL, ...) { | |
o = -log10(sort(pvector,decreasing=F)) | |
e = -log10( 1:length(o)/length(o) ) | |
plot(e,o,pch=19,cex=1, main=main, ..., | |
xlab=expression(Expected~~-log[10](italic(p))), | |
ylab=expression(Observed~~-log[10](italic(p))), | |
xlim=c(0,max(e)), ylim=c(0,max(e))) | |
lines(e,e,col="red") | |
} | |
#Generate some fake data that deviates from the null | |
set.seed(42) | |
pvalues=runif(10000) | |
pvalues[sample(10000,10)]=pvalues[sample(10000,10)]/5000 | |
# pvalues is a numeric vector | |
pvalues[1:10] | |
# Using the ggd.qqplot() function | |
ggd.qqplot(pvalues) | |
# Add a title | |
ggd.qqplot(pvalues, "QQ-plot of p-values using ggd.qqplot") |
The Perl highlighter also works well. Here's some code I posted recently to help clean up PLINK output:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# cleanplink.pl | |
# (c) Stephen D. Turner 2010 http://www.stephenturner.us/ | |
# This is free open-source software. | |
# See http://gettinggeneticsdone.blogspot.com/p/copyright.html | |
my $help = "\nUsage: $0 <input whitespace file> <tab or comma>\n\n"; | |
die $help if @ARGV<2; | |
$delimiter=pop(@ARGV); | |
die $help unless ($delimiter=~/tab/i|$delimiter=~/comma/i); | |
@inputfiles=@ARGV; | |
if ($delimiter =~ /comma/i) { | |
foreach (@inputfiles) { | |
open (IN,"<$_"); | |
open (OUT,">$_.csv"); | |
while (<IN>) { | |
chomp; | |
$_ =~ s/^\s+//; #Trim whitespace at beginning | |
$_ =~ s/\s+$//; #Trim whitespace at end | |
$_ =~ s/\s+/,/g; #Remaining whitespace into commas | |
#$_ =~ s/NA/-9/g;#If you want to recode NA as -9 | |
print OUT "$_\n"; | |
} | |
} | |
} elsif ($delimiter =~ /tab/i) { | |
foreach (@inputfiles) { | |
open (IN,"<$_"); | |
open (OUT,">$_.tab"); | |
while (<IN>) { | |
chomp; | |
$_ =~ s/^\s+//; #Trim whitespace at beginning | |
$_ =~ s/\s+$//; #Trim whitespace at end | |
$_ =~ s/\s+/\t/g;#Remaining whitespace into commas | |
#$_ =~ s/NA/-9/g;#If you want to recode NA as -9 | |
print OUT "$_\n"; | |
} | |
} | |
} else { | |
die $help; | |
} |
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'.