Skip to content

Instantly share code, notes, and snippets.

@lgatto
Created May 13, 2012 20:40
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 lgatto/2690073 to your computer and use it in GitHub Desktop.
Save lgatto/2690073 to your computer and use it in GitHub Desktop.
Monitor a variable in R
## Credit Hadley Wickham
## http://www.mail-archive.com/r-help@r-project.org/msg125980.html
watch <- function(varname) {
old <- get(varname)
changed <- function(...) {
new <- get(varname)
if (!identical(old, new)) {
message(varname, " is now ", new)
old <<- new
}
TRUE
}
invisible(addTaskCallback(changed))
}
> a <- 1
> watch("a")
> a <- 2
a is now 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment