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){
    statis...@obs = c(statis...@obs, newData)
    statis...@max = max(newData, statis...@max, na.rm=T)
    statis...@min = min(newData, statis...@min, na.rm=T)
    statis...@avg = mean(statis...@obs)
    statis...@current = newData
    if(length(statis...@obs)==1 || is.na(statis...@beg)){
        statis...@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){
    statis...@obs = c(statis...@obs, newData)
    statis...@max = max(newData, statis...@max, na.rm=T)
    statis...@min = min(newData, statis...@min, na.rm=T)
    statis...@avg = mean(statis...@obs)
    statis...@current = newData
    if(length(statis...@obs)==1 || is.na(statis...@beg)){
        statis...@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.

Reply via email to