Skip to content

Instantly share code, notes, and snippets.

@vst
Created February 8, 2012 03:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vst/1765098 to your computer and use it in GitHub Desktop.
Save vst/1765098 to your computer and use it in GitHub Desktop.
Retrieves and evaluates remote R sources including those served over secure http (https) connections. Requires RCurl package.
##' Retrieves and evaluates a remote R source file.
##'
##' @param file defines a URL as the remote source file path
##' @param local indicates if the remote source should be evaluated in
##' the environment which calls remoteSource. If FALSE (default),
##' evaluated in the global environment.
##' @param verbose indicates if verbose output should be printed.
##' @param echo indicates if the source should be echoed before
##' evaluation.
##' @return result of evaluating the remote source.
remoteSource <- function(file, local=FALSE, verbose=FALSE, echo=FALSE) {
## Define verbose output method:
.ccat = function (...) {
if (verbose) {
cat(...)
}
}
## We need the RCurl for remote source served over https:
require(RCurl)
## Attempt to get the remote source:
.ccat("Attempting to retrieve the remote source...\n")
csource <- getURL(file)
.ccat("Remote source retrieved...\n")
## Prepare arguments:
.ccat("Parsing the source...\n")
args = list(expr=parse(text=csource))
if (local) {
args = append(args, envir=.GlobalEnv)
}
## Done, evaluate the source:
.ccat("Evaluating the source...\n")
if (echo) {
cat(sprintf("%s\n", csource))
}
return(do.call(eval, args))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment