[R] detecting if a variable has changed

Neal H. Walfield neal at walfield.org
Sun Jun 5 15:53:50 CEST 2016


Hi,

I have a huge list.  Normally it is sorted, but I want to be able to
add elements to it without having to use any special interfaces and
then sort it on demand.  My idea is to use something like weak
references combined with attributes.  Consider:

  # Initialization.
  l = as.list(1:10)
  # Note that it is sorted.
  attr(l, 'sorted') = weakref(l)

  # Modify the list.
  l = append(l, 1:3)

  # Check if the list is still sorted.  (I use identical here, but it
  # probably too heavy weight: I just need to compare the addresses.)
  if (! identical(l, attr(l, 'sorted'))) {
    l = sort(unlist(l))
    attr(l, 'sorted') = weakref(l)
  }
  # Do operation that requires sorted list.
  ...

This is obviously a toy example.  I'm not actually sorting integers
and I may use a matrix instead of a list.

I've read:

  http://www.hep.by/gnu/r-patched/r-exts/R-exts_122.html
  http://homepage.stat.uiowa.edu/~luke/R/references/weakfinex.html

As far as I can tell, weakrefs are only available via the C API.  Is
there a way to do what I want in R without resorting to C code?  Is
what I want to do better achieved using something other than weakrefs?

Thanks!

:) Neal



More information about the R-help mailing list