Sunday, September 20, 2015

R-Function to Source all Functions from a GitHub Repository

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 = "onlinetrickpdf-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()

No comments:

Post a Comment