Hadley, thanks for your comment. What you're saying is true, of course, and possibly I did not really chose the best header to describe the actual issue I was addressing. However, in the help archive I found something that gets me what I want (calling a certain "slot function" without having to specify arguments at calling time). I adapted it to my situation and maybe it's of interest for someone facing a similar problem.
setClass( Class="Testclass", representation=representation( env="environment", data="function" ) ) data.fun <- function(env) get("actual.data", env=env) setMethod( f="initialize", signature=signature("Testclass"), definition=function( .Object, actual.data=NULL, ... ) { env <- new.env(parent=emptyenv()) env$actual.data <- actual.data data.fun.wrapper <- function() data.fun(env) callNextMethod(.Object, env=env, data=data.fun.wrapper, ...) } ) obj <- new("Testclass", actual.data=1:10) o...@data() o...@env$actual.data <- 1:5 o...@data() obj.mod <- new("Testclass") obj....@env$actual.data <- "horst" obj....@data() Loading an object from harddrive could be implemented in a similar way by putting the path-filename-combination in "o...@env" and adapting the function "data.fun()" accordingly. Putting "data.fun()" into the wrapper "data.fun.wrapper()" allows for changes in "data.fun()" without having to re-source the method for "initialize()" and to reassign "obj" each time. Regards, Janko > -----Ursprüngliche Nachricht----- > Von: hadley wickham [mailto:h.wick...@gmail.com] > Gesendet: Freitag, 15. Januar 2010 19:35 > An: Janko Thyson > Cc: r-help@r-project.org > Betreff: Re: [R] Can an object reference itself? > > On Fri, Jan 15, 2010 at 5:30 PM, Janko Thyson > <janko.thy...@ku-eichstaett.de> wrote: > > Dear List, > > > > I am not really familiar with any other language than R, but Ive > heard that > > in other languages there is something called self referencing. > > > > Heres what Im trying to get an answer for: > > Suppose there is a function that takes as its input a value of a slot > of an > > S4 object. The function itself is stored in another slot of the SAME > S4 > > object. Is it then possible to have the function automatically > recognize > > the name of the object in which slot it is placed via some sort of > self > > referencing of the S4 object (to avoid having to explicitly state > the > > required function argument for different S4 object instances with > different > > names)? > > > > I hope the following code snippets will give you an idea what Im > trying to > > do: > > > > obj.for.slot <- data.frame(a=1); obj.for.slot <- data.frame(a=100) > > save(obj.for.slot, file="C:/obj.1.for.slot.Rdata"); > save(obj.for.slot, > > file="C:/obj.2.for.slot.Rdata") > > > > slotfun <- function(obj.name) > > { > > file.fqn.char <- paste("file.fqn <- ", obj.name, "@file.fqn", > > sep="") > > eval(parse(text=file.fqn.char)) > > load(file=file.fqn) > > return(obj.for.slot) > > } > > > > setClass( > > Class="Testclass", > > representation=representation( > > file.fqn="character", > > data="function" > > ), > > prototype=prototype( > > file.fqn="blabla", > > data=slotfun > > ) > > ) > > > > test <- new("Testclass") > > test.mod <- new("Testclass") > > > > > > t...@file.fqn <- "C:/obj.1.for.slot.Rdata" > > test....@file.fqn <- "C:/obj.2.for.slot.Rdata" > > > > t...@data(obj.name="test") > > test....@data(obj.name="test.mod") > > An object does not have a unique name. What would happen in the > following situations? > > t3 <- test.mod > tests <- list( > test, > test.mod > ) > > Hadley > > -- > http://had.co.nz/ ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.