Created
February 8, 2012 03:30
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##' 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