Hello all, I wrote a function with many arguments. Then I need to call it many times with changes on some arguments only. Is there any way to write a function or have a method to "update" it, like the relationship between lm() and update()? Many thanks, Edwin Sun ------------ This is the sample code.> test <- function(y, z) {+ x <- y +1 + w <- z * 2 + result <- list(x=x, w=w) + class(result) <- "ego" + return(result) + }> me <- test(y=3, z=4); me$x [1] 4 $w [1] 8 attr(,"class") [1] "ego"> update(me, y=5)Error in update.default(me, y = 5) : need an object with call component -- View this message in context: http://r.789695.n4.nabble.com/how-to-update-my-own-function-tp3056256p3056256.html Sent from the R help mailing list archive at Nabble.com.
On 23/11/2010 4:21 PM, Edwin Sun wrote:> > Hello all, > > I wrote a function with many arguments. Then I need to call it many times > with changes on some arguments only. Is there any way to write a function or > have a method to "update" it, like the relationship between lm() and > update()? > > Many thanks, > > Edwin Sun > > ------------ > This is the sample code. > >> test<- function(y, z) { > + x<- y +1 > + w<- z * 2 > + result<- list(x=x, w=w) > + class(result)<- "ego" > + return(result) > + } > >> me<- test(y=3, z=4); me > $x > [1] 4 > > $w > [1] 8 > > attr(,"class") > [1] "ego" > >> update(me, y=5) > Error in update.default(me, y = 5) : need an object with call componentChange the result line to result<- list(x=x, w=w, call=sys.call()) and the default update method should do what you want. If it doesn't, you can write an update.ego method to do something differently. Duncan Murdoch>
Edwin - I think the usual way to do this would be to use a function like lapply or mapply to call your function multiple times with varying arguments. For example, with one varying argument: lapply(list(3,5), test, z = 4) With multiple varying arguments: mapply(test, y = list(3,5), z = list(4, 8)) See ?lapply and ?mapply, and the MoreArgs and SIMPLIFY arguments to mapply. --Erik Edwin Sun wrote:> Hello all, > > I wrote a function with many arguments. Then I need to call it many times > with changes on some arguments only. Is there any way to write a function or > have a method to "update" it, like the relationship between lm() and > update()? > > Many thanks, > > Edwin Sun > > ------------ > This is the sample code. > >> test <- function(y, z) { > + x <- y +1 > + w <- z * 2 > + result <- list(x=x, w=w) > + class(result) <- "ego" > + return(result) > + } > >> me <- test(y=3, z=4); me > $x > [1] 4 > > $w > [1] 8 > > attr(,"class") > [1] "ego" > >> update(me, y=5) > Error in update.default(me, y = 5) : need an object with call component >
Thank you all the reply. The use of sys.call() as suggested by Ducan works pretty well. This is good as the outputs from my function are large. The use of apply family functions as suggested by Erik is good for small amounts of outputs. Edwin Sun -- View this message in context: http://r.789695.n4.nabble.com/how-to-update-my-own-function-tp3056256p3056301.html Sent from the R help mailing list archive at Nabble.com.