[R] monitor variable change

Hadley Wickham hadley at rice.edu
Wed Feb 16 15:29:46 CET 2011


One way to implement this functionality is with a task manager callback:

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


Hadley

On Wed, Feb 16, 2011 at 9:38 AM, Alaios <alaios at yahoo.com> wrote:
> Dear all I would like to ask you if there is a way in R to monitor in R when a value changes.
>
> Right now I use the sprintf('my variables is %d \n, j) to print the value of the variable.
>
> Is it possible when a 'big' for loop executes to open in a new window to dynamically check only the variable I want to.
>
> If I put all the sprintf statements inside my loop then I get flooded with so many messages that makes it useless.
>
> Best Regards
> Alex
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/



More information about the R-help mailing list