Hi Rob,
I just started reading about classes (and also learning R), so I apologize if
the following code is confusing you more. I simplified the code somewhat in
order to better understand what's going on. I was wondering: are you
deliberately reimplementing the builtin update() function?
setClass(Class="StatisticInfo",
representation( oldData = "data.frame",
newData = "data.frame"
)
)
# declare the update method, even though it exists already.
setGeneric (
name="update",
def=function(object){standardGeneric("update")}
)
setMethod(f="update", signature ("StatisticInfo"),
definition = function(object){
min = min(object@newData, object@oldData, na.rm=T)
avg = mean(mean(cbind(object@newData, object@oldData)))
max = max(object@newData, object@oldData, na.rm=T)
return(list(min, avg, max))
}
)
old <- data.frame(runif(10, 1, 10))
new <- data.frame(runif(10, 1, 9))
instance <- new(Class="StatisticInfo", oldData=old, newData=new)
update(instance)
Does this make sense to you?
Cheers!!
Albert-Jan
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the face of ambiguity, refuse the temptation to guess.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- On Tue, 3/9/10, Rob Forler <rforler@uchicago.edu> wrote:
From: Rob Forler <rforler@uchicago.edu>
Subject: [R] confused by classes and methods.
To: r-help@r-project.org
Date: Tuesday, March 9, 2010, 12:09 AM
Hello, I have a simple class that looks like:
setClass("statisticInfo",
representation( max = "numeric",
min = "numeric",
beg = "numeric",
current = "numeric",
avg = "numeric",
obs = "vector"
)
)
and the following function
updateStatistic <- function(statistic, newData){
statistic@obs = c(statistic@obs, newData)
statistic@max = max(newData, statistic@max, na.rm=T)
statistic@min = min(newData, statistic@min, na.rm=T)
statistic@avg = mean(statistic@obs)
statistic@current = newData
if(length(statistic@obs)==1 || is.na(statistic@beg)){
statistic@beg = newData
}
return(statistic)
}
Firstly,
I know you can use methods which seems to add some value. I looked at
http://developer.r-project.org/methodDefinition.html but I try
setMethod("update", signature(statistic="statisticInfo",
newData="numeric"),
function(statistic, newData){
statistic@obs = c(statistic@obs, newData)
statistic@max = max(newData, statistic@max, na.rm=T)
statistic@min = min(newData, statistic@min, na.rm=T)
statistic@avg = mean(statistic@obs)
statistic@current = newData
if(length(statistic@obs)==1 || is.na(statistic@beg)){
statistic@beg = newData
}
return(statistic)
}
)
Creating a new generic function for "update" in ".GlobalEnv"
Error in match.call(fmatch, fcall) :
unused argument(s) (statistic = "statisticInfo", newData =
"numeric")
1: source("tca.init.R", chdir = T)
2: eval.with.vis(ei, envir)
3: eval.with.vis(expr, envir, enclos)
4: source("../../studies/tca.tradeClassifyFuncs.R")
5: eval.with.vis(ei, envir)
6: eval.with.vis(expr, envir, enclos)
7: setMethod("update", signature(statistic =
"statisticInfo", newData "numeric"), function(statistic,
newData) {
8: isSealedMethod(f, signature, fdef, where = where)
9: getMethod(f, signature, optional = TRUE, where = where, fdef = fGen)
10: matchSignature(signature, f
I don't understand this any help would be appreciated.
Secondly, can anyone give any examples of where methods are used that makes
sense besides just checking the class inputs?
Thirdly, I've looked into passing by reference in R, and some options come
up, but in general they seem to be fairly complicated.
I would like update to work more like my update function to work without
having to return a a new object.
Something like> statList = list(new("statisticInfo"))
> updateStatistic(statList[[1]],3)
> statList[[1]]
#this would then have the updated one and not the old one.
Anyways,
The main reason I'm asking these questions is because I can't really
find a
good online resource for this. Any help would be greatly appreciated.
Thanks,
Rob
[[alternative HTML version deleted]]
______________________________________________
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.
[[alternative HTML version deleted]]