Reading an R file from GitHub

[This article was first published on Fellgernon Bit - rstats, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Lets say that I want to read in this R file from GitHub into R.

The first thing you have to do is locate the raw file. You can do so by clicking on the Raw button in GitHub. In this case it’s https://raw.github.com/lcolladotor/ballgownR-devel/master/ballgownR/R/infoGene.R

One would think that using source() would work, but it doesn’t as shown below:

source("https://raw.github.com/lcolladotor/ballgownR-devel/master/ballgownR/R/infoGene.R")

## Warning: unsupported URL scheme

## Error: cannot open the connection

However, thanks again to Hadley Wickham you can do so by using the devtools (Wickham & Chang, 2013 ) package.

Here is how it works:

library(devtools)
library(roxygen2)
## Needed because this file has roxygen2 comments. Otherwise you get a
## 'could not find function 'digest'' error
source_url("https://raw.github.com/lcolladotor/ballgownR-devel/master/ballgownR/R/infoGene.R")

## SHA-1 hash of file is 6c32a620799eded5d6ff0997a184843d7964724a

## Note that you can specify the SHA-1 hash to be very specific about
## which version of the file you want to read in.

We can then check that infoGene has actually been sourced:

"infoGene" %in% ls()

## [1] TRUE

That’s it! Enjoy!

Citations made with knitcitations (Boettiger, 2013 ).

Reproducibility

sessionInfo()

## R version 3.0.0 (2013-04-03)
## Platform: x86_64-apple-darwin10.8.0 (64-bit)
## 
## locale:
## [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] roxygen2_2.2.2      digest_0.6.3        devtools_1.2       
## [4] knitcitations_0.4-6 bibtex_0.3-5        knitr_1.2          
## 
## loaded via a namespace (and not attached):
##  [1] brew_1.0-6     evaluate_0.4.3 formatR_0.7    httr_0.2      
##  [5] memoise_0.1    parallel_3.0.0 RCurl_1.95-4.1 stringr_0.6.2 
##  [9] tools_3.0.0    whisker_0.3-2  XML_3.95-0.2   xtable_1.7-1

To leave a comment for the author, please follow the link and comment on their blog: Fellgernon Bit - rstats.

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.

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)