[Bioc-devel] Lazy evaluation and S4 methods

Kevin Ushey kevinushey at gmail.com
Wed Apr 23 20:02:09 CEST 2014


Hi everyone,

I'm trying to investigate lazy evaluation with S4, and specifically
the environment chain available for lookup when an S4 method is
evaluated.

With 'vanilla' R functions, we can write something like the following:

    lazy <- function(...) {
      call <- substitute(list(...))
      for (i in 2:length(call)) {
        eval(call[[i]])
      }
    }

and evaluation will succeed if this function is called by another function:

    f <- function() {
      var <- 10
      lazy(x = var)
    }

    f()

The evaluation of the symbol 'var' is able to find it in the 'f' environment.

    setClass("Lazy", list(data="list"))
    setGeneric("lazy", function(x, ...) standardGeneric("lazy"))
    setMethod("lazy", list(x="Lazy"), function(x, ...) {
      call <- substitute(list(...))
      for (i in 2:length(call)) {
        eval( call[[i]] )
      }
    })

    f <- function() {
      var <- new("Lazy", data=list(1))
      y <- 1
      cat("\n")
      lazy(var, a=y)
    }

    f()

gives ' Error in eval(expr, envir, enclos) : object 'y' not found '.
It seems like the S4 method is evaluated in an environment detached
from the scope of the caller (which is probably outlined in ?Methods,
but I am having trouble parsing everything in there).

My main question is -- is there a nice way to work around this, in a
way that is transparent to the user of a function?

Further motivation available in the GitHub issue for flowCore here:
https://github.com/RGLab/ncdfFlow/issues/31

Thanks,
Kevin



More information about the Bioc-devel mailing list