R-Function to Source all Functions from a GitHub Repository

January 1, 2012
By

(This article was first published on theBioBucket*, and kindly contributed to R-bloggers)

Here's a function that sources all scripts from an arbitrary github-repository. At the moment the function downloads the whole repo and sources functions kept in a folder named "Functions" - this may be adapted for everyones own purpose.










# Script name: fun_install_github.R
# Purpose: Source all functions from a GitHub repo
# Author: Kay Cichini
# Date: 2012-01-01
# Credit: This is a hacked version of Brad Hadley's install_github function at
# https://github.com/hadley/devtools/blob/master/R/install-github.r
# Packages needed: RCurl
# Arguments: repo, username, branch
# Specification: I use a folder in GitHub to hold functions only

fun_install_github <- function (repo = "theBioBucket-Archives",
username = "gimoya",
branch = "master")
{require(RCurl)
message("\nInstalling ", repo, " R-functions from user ", username)
name <- paste(username, "-", repo, sep = "")
url <- paste("https://github.com/", username, "/", repo,
sep = "")
zip_url <- paste("https://nodeload.github.com/", username,
"/", repo, "/zipball/", branch, sep = "")
src <- file.path(tempdir(), paste(name, ".zip", sep = ""))
content <- getBinaryURL(zip_url, .opts = list(followlocation = TRUE,
ssl.verifypeer = FALSE))
writeBin(content, src)
on.exit(unlink(src), add = TRUE)
repo_name <- basename(as.character(unzip(src, list = TRUE)$Name[1]))
out_path <- file.path(tempdir(), repo_name)
unzip(src, exdir = tempdir())
on.exit(unlink(out_path), add = TRUE)
fun.path <- dir(paste(out_path, "/R/Functions/", sep = ""), full.names = T)
for (i in 1:length(fun.path)) {
source(fun.path[i])
cat("\n Sourcing function: ", dir(paste(out_path, "/R/Functions/", sep = ""))[i])}
cat("\n")
}

# Example:
# fun_install_github()

To leave a comment for the author, please follow the link and comment on his blog: theBioBucket*.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: 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...

Tags: ,

Comments are closed.