Skip to content

Instantly share code, notes, and snippets.

@nmatzke

nmatzke/sourceall_git.R

Last active Sep 28, 2015
Embed
What would you like to do?
# Source ALL of the R files in a particular R/ directory
# (so we don't have to have a fully-functioning package)
# Author: Nick Matzke, nmatzke
# License: GPL-3
sourceall_git <- function(repo)
{
library(httr) # for GET
library(devtools) # for source_url
# Based on:
# http://stackoverflow.com/questions/25485216/how-to-get-list-files-from-a-github-repository-folder-using-r
git_api = paste0("https://api.github.com/repos/", repo, "/git/trees/master?recursive=1")
req <- httr::GET(git_api)
httr::stop_for_status(req)
# Find the .R files
filelist <- unlist(lapply(content(req)$tree, "[", "path"), use.names = F)
Rfiles = grep(".R", filelist, value = TRUE, fixed = TRUE)
# Remove .Rproj, add
keepTF = grepl(pattern=".Rproj", x=Rfiles) == FALSE
Rfiles_fns = Rfiles[keepTF]
rawurl = paste0("https://raw.githubusercontent.com/", repo, "/master/")
Rfiles = paste0(rawurl, Rfiles_fns)
cat("\nAttempting to source all *.R files in ", rawurl, ":\n", sep="")
for (i in 1:length(Rfiles))
{
cat("Sourcing ", Rfiles[i], " ...\n", sep="")
devtools::source_url(Rfiles[i])
}
cat("\n...done.")
return(Rfiles)
} # END sourceall_git
#######################################################
# sourceall
####################################ex###################
#' Source all .R files in a directory, except "compile" and "package" files
#'
#' Utility function.
#'
#' @param path The path to source
#' @param pattern Default is .R
#' @param ... Additional arguments to source
#' @return \code{path} The path that was sourced.
#' @export
#' @seealso \code{\link[base]{source}}
#' @note Go BEARS!
#' @author Nicholas J. Matzke \email{matzke@@berkeley.edu}
#' @references
#' \url{http://phylo.wikidot.com/matzke-2013-international-biogeography-society-poster}
#' \url{https://code.google.com/p/lagrange/}
#' @bibliography /Dropbox/_njm/__packages/BioGeoBEARS_setup/BioGeoBEARS_refs.bib
#' @cite Matzke_2012_IBS
#' @examples
#' test=1
sourceall <- function(path=path, pattern="\\.R", ...)
{
tmppath = np(addslash(path))
Rfiles = list.files(path=tmppath, pattern="\\.R", ...)
# Files to remove
Rfiles_remove_TF1 = grepl("compile", Rfiles)
Rfiles_remove_TF2 = grepl("package", Rfiles)
Rfiles_remove_TF3 = grepl("\\.Rproj", Rfiles)
Rfiles_remove_TF = (Rfiles_remove_TF1 + Rfiles_remove_TF2 + Rfiles_remove_TF3) >= 1
Rfiles = Rfiles[Rfiles_remove_TF == FALSE]
cat("\nSourcing Rfiles in ", path, "...\n", sep="")
for (Rfile in Rfiles)
{
cat("Sourcing Rfile: ", Rfile, "\n", sep="")
fullfn = np(slashslash(paste(addslash(path), Rfile, sep="")))
source(fullfn, chdir=TRUE, ...)
}
cat("\nDone sourcing Rfiles in ", path, "...\n", sep="")
return(path)
} # END sourceall
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.