R-Function to Source all Functions from a GitHub Repository

[This article was first published on theBioBucket*, 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.

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")
  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)) {
    cat("\n Sourcing function: ", dir(paste(out_path, "/R/Functions/", sep = ""))[i])}

# Example:
# fun_install_github()

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

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)