Zhiqiu Hu
2012-Aug-30 22:07 UTC
[R] How to modify the values of the parameters passing via ...
Dear Friends, Let's assume there are three parameters that were passed into fun1. In fun1, we need to modify one para but the remains need to be untouched. And then all parameters were passed into fun2. However, I have failed to achieve it. Please see the following code. ########################################## fun1 <-function(x, y, z=10) {x+y+z;} fun2 <-function(aa, ...) { a=fun1(x=aa, y=5); # works well a=1+5+10=16 b=fun1(x=aa, ...); # works well b=1+4+8=13 y=0 # it will not affect values passed using ... c=fun1(x=aa, ...) # works well but c = 1+4+8=13 # Some time we may only want to use parts of the inputed variables # we only want to modify y d=fun1(x=aa, y=5, ...); #error : formal argument "y" matched by multiple actual arguments #How can I get the result d=1+5+8=14? c(a, b, c) } fun2(x=1, y=4, z=8) ########################################## I will appreciate it if you would give me any suggestions. Best wishes, Zhiqiu [[alternative HTML version deleted]]
Rui Barradas
2012-Aug-30 22:58 UTC
[R] How to modify the values of the parameters passing via ...
Hello, You could see what is fun1 receiving with this modification meant for debugging: fun1 <-function(x, y, z=10){ print(match.call()) x + y + z } Anyway, there is s standard way of dealing with argument '...' when one needs to know what was passed. Include dots <- list(...) at the beginning of fun2 and then use it when necessary: fun1 <-function(x, y, z=10){x + y + z} fun2 <-function(aa, ...) { dots <- list(...) a <- fun1(x=aa, y=5) # works well a=1+5+10=16 b <- fun1(x=aa, ...) # works well b=1+4+8=13 y <- 0 # it will not affect values passed using ... c <- fun1(x=aa, ...) # works well but c = 1+4+8=13 d <- fun1(x=aa, y=5, dots$z) # works well d=1+5+8=14 c(a, b, c, d) } fun2(aa=1, y=4, z=8) [1] 16 13 13 14 Also, to end statements with ';' is a C language tique, R doesn't need it. (Nor it hurts.) Hope this helps, Rui Barradas Em 30-08-2012 23:07, Zhiqiu Hu escreveu:> Dear Friends, > > Let's assume there are three parameters that were passed into fun1. In > fun1, we need to modify one para but the remains need to be untouched. And > then all parameters were passed into fun2. However, I have failed to > achieve it. > > Please see the following code. > > ########################################## > fun1 <-function(x, y, z=10) {x+y+z;} > > fun2 <-function(aa, ...) { > a=fun1(x=aa, y=5); # works well a=1+5+10=16 > b=fun1(x=aa, ...); # works well b=1+4+8=13 > y=0 # it will not affect values passed using ... > c=fun1(x=aa, ...) # works well but c = 1+4+8=13 > > # Some time we may only want to use parts of the inputed variables > # we only want to modify y > d=fun1(x=aa, y=5, ...); #error : formal argument "y" matched by multiple > actual arguments > #How can I get the result d=1+5+8=14? > > c(a, b, c) > } > > fun2(x=1, y=4, z=8) > ########################################## > > I will appreciate it if you would give me any suggestions. > > Best wishes, > > Zhiqiu > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.