Created
June 17, 2012 23:30
-
-
Save ssimeonov/2946052 to your computer and use it in GitHub Desktop.
Rscript discovery & reloading
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
# 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