Skip to content

Instantly share code, notes, and snippets.

@lee-dohm
Forked from ssimeonov/rscript.R
Created July 18, 2012 18:57
Show Gist options
  • Save lee-dohm/3138082 to your computer and use it in GitHub Desktop.
Save lee-dohm/3138082 to your computer and use it in GitHub Desktop.
Rscript discovery & reloading
rscript.stack <- function()
{
# Returns the stack of Rscript files.
#
# Returns:
# Stack of files.
Filter(Negate(is.null), lapply(sys.frames(), function(x) x$ofile))
}
rscript.current <- function()
{
# Returns the path of the current file.
#
# Returns:
# Path to the current file.
stack <- rscript.stack()
as.character(stack[length(stack)])
}
rscript.reloader <- function(func.name, notify=TRUE)
{
# Creates a function in the top environment to reload the current script.
#
# Args:
# func.name: Name for the reloader function to take.
# notify: Prints a notification of what the reloader function will do when called.
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