Dear List, I am not really familiar with any other language than R, but I?ve heard that in other languages there is something called ?self referencing?. Here?s what I?m 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 I?m 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") test at file.fqn <- "C:/obj.1.for.slot.Rdata" test.mod at file.fqn <- "C:/obj.2.for.slot.Rdata" test at data(obj.name="test") test.mod at data(obj.name="test.mod") I'm trying to have "slotfun()" be stated in a way that does not require an explicit stating of argument "obj.name"): test at data() test.mod at data() Any hints in the right directions greatly appreciated! Regards, Janko Thyson janko.thyson at kuei.de Catholic University of Eichst?tt-Ingolstadt Ingolstadt School of Management Statistics and Quantitative Methods Auf der Schanz 49 D-85049 Ingolstadt www.wfi.edu/lsqm Fon:? +49 841 937-1923 Fax:? +49 841 937-1965
On Fri, Jan 15, 2010 at 5:30 PM, Janko Thyson <janko.thyson at ku-eichstaett.de> wrote:> Dear List, > > I am not really familiar with any other language than R, but I?ve heard that > in other languages there is something called ?self referencing?. > > Here?s what I?m 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 I?m 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") > > > test at file.fqn ? ? ? ? ? <- "C:/obj.1.for.slot.Rdata" > test.mod at file.fqn ? ? ? <- "C:/obj.2.for.slot.Rdata" > > test at data(obj.name="test") > test.mod at 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/
Maybe Matching Threads
- Function "nsl()" missing in package utils
- Problems building own package (Error: "package has been build before R-2.10.0")
- Problems building own package (Error: "package has been build before R-2.10.0")
- Feature request: extend functionality of 'unlist()' by args 'delim=c("/", "_", etc.)' and 'keep.special=TRUE/FALSE'
- Query super- and subclasses of a class: is there a better way than to use 'completeClassDefinition()'