Sourcing Code from GitHub
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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 library(ggplot2) Plot <- qplot(cars$dist, cars$speed) + theme_bw() print(Plot)
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:
library(devtools) SourceURL <- "https://raw.github.com/christophergandrud/christophergandrud.github.com/master/SourceCode/CarsScatterExample.R" source_url(SourceURL)
There you go.
You can also directly source GitHub gists (which are nice for sharing short bits of code) with the source_gist
command.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.