10 July 2012

Sourcing Code from GitHub

In previous posts I described how to input data stored on GitHub directly into R.

You can do the same thing with source code stored on GitHub. Hadley Wickham has actually made the whole process easier by combining the getURL, textConnection, and source commands into one function: source_url. This is in his devtools package.

Imagine we have a .R source code file like this:

# Make cars scatter plot
Plot <- qplot(cars$dist, cars$speed) +

It is hosted on GitHub with the URL: https://raw.github.com/christophergandrud/christophergandrud.github.com/master/SourceCode/CarsScatterExample.R

So to run this source code directly in R all we need to type is:

SourceURL <- "https://raw.github.com/christophergandrud/christophergandrud.github.com/master/SourceCode/CarsScatterExample.R"

plot of chunk CarsPlot

There you go.

You can also directly source GitHub gists (which are nice for sharing short bits of code) with the source_gist command.


Hadley Wickham said...

Hmmmm, do we need a source_github function to make this a little less verbose?

Christopher Gandrud said...

You mean combining source_url and source_gist into a new source_github function? That would definitely cut down on the clutter.

Also, I was thinking of using your source_url code as the basis of a read_github function. It would do the same thing, but instead of sourcing the file it would send it to read.table. This would be helpful for using GitHub as a data repository.