[Rd] S4 class, passing argument names to function, modify original

soeren.vogel at uzh.ch soeren.vogel at uzh.ch
Sat Jun 4 12:07:00 CEST 2011


Hello, an S4 class "Foo" is defined with a setter, $. For several reasons, the setter calls a function, .foo.update(). However, bypassing the argument names of the setter does not work. Question 1: Why not and how can I fix this? Question 2: What is the way to define either the function or the setter to modify the original object (not returning the modified copy of it an overwrite the original by assignment)? Thanks, Sören

setClass("Foo",
  representation(
    N = "numeric"
  ),
  prototype(
    N = 10000
  )
)

.foo.update <- function(object, ...) {
  args <- list(...)
  for (i in slotNames("Foo")[pmatch(names(args), slotNames("Foo"), nomatch=0)]) {
    slot(object, i) <- args[[i]]
    # indeed more to do here
    return(object)
  }
}

setReplaceMethod("$", "Foo",
  function(x, name, value) {
    x <- .foo.update(x, name=value)
    x
  }
)

x <- new("Foo")
x
x$N <- 99
x # NULL????



More information about the R-devel mailing list