Search code examples
rreference-class

Evaluation nested too deeply when using reference class with active binding


Reproducible example:

bed2_RC <- methods::setRefClass(

  "bed2",

  fields = list(
    .fam = "data.frame",

    #### Active bindings
    fam = function() {
      if (ncol(.self$.fam) == 0) {
        .self$.fam <- datasets::iris
      }
      .self$.fam
    },

    nrow = function() print(nrow(.self$fam))
  )
)

bed2 <- function() new(Class = "bed2")
bed2()

Error message:

Error: evaluation nested too deeply: infinite recursion / options(expressions=)?
Error during wrapup: evaluation nested too deeply: infinite recursion / options(expressions=)?

I don't get why there is an infinite loop happening here.


Solution

  • Hum, when using nrow(.self$fam), it actually tries to call the active binding $nrow(), not the base function. Replacing with base::nrow(.self$fam) works as expected.