Sourcing Code from GitHub

July 10, 2012

(This article was first published on Christopher Gandrud (간드루드 크리스토파), and kindly contributed to R-bloggers)

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:

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

SourceURL <- ""

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.

To leave a comment for the author, please follow the link and comment on their blog: Christopher Gandrud (간드루드 크리스토파). offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)