From soeren.vogel at uzh.ch  Sat Jun  4 12:07:00 2011
From: soeren.vogel at uzh.ch (soeren.vogel at uzh.ch)
Date: Sat, 4 Jun 2011 12:07:00 +0200
Subject: [Rd] S4 class, passing argument names to function, modify original
Message-ID: <C6C4A88B-761A-46F0-90E3-24260973F699@uzh.ch>

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????



[Rd] S4 class, passing argument names to function, modify original
soeren.vogel at uzh.ch