Skip to content

Instantly share code, notes, and snippets.

@ssimeonov
Created June 17, 2012 23:30
Show Gist options
  • Save ssimeonov/2946052 to your computer and use it in GitHub Desktop.
Save ssimeonov/2946052 to your computer and use it in GitHub Desktop.
Rscript discovery & reloading
# Returns the stack of RScript files
rscript.stack <- function() {
Filter(Negate(is.null), lapply(sys.frames(), function(x) x$ofile))
}
# Returns the current RScript file path
rscript.current <- function() {
stack <- rscript.stack()
as.character(stack[length(stack)])
}
# Creates a function in the top environment to reload the current script
rscript.reloader <- function(func.name, notify=TRUE) {
func.name <- as.character(func.name)
path <- rscript.current()
if (notify) {
cat(paste('Invoking ', func.name, '() will source ', path, ' in globalenv().\n', sep=''))
}
reloader <- function() {
source(path, local=globalenv())
}
assign(func.name, reloader, envir=globalenv())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment