Pages

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.

# 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")
view raw ggd_qqplot.r hosted with ❤ by GitHub


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

#!/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;
}
view raw gistfile1.pl hosted with ❤ by GitHub


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.