thmsfuller066 at gmail.com
2010-Jul-14 05:39 UTC
[R] How to define a function (with '<-') that has two arguments?
Hi All, The last line if the following code returns the error right below this paragraph. Essentially, I use the operator %:% to retrieve a variable in a nested frame. Then I want to use the same operator (with '<-') to change its value. I'm not sure if this is possible in R with %:% operator. Would you please help and let me how to correct the code if it is possible? Error in `%:%<-`(`*tmp*`, a, value = 2) : unused argument(s) (value = 2) #####code### `%:%`=function(x,y) { if(!is.environment(x)) { x=as.character(substitute(x)) x=get(x) } y=as.character(substitute(y)) get(y, envir=x) } assign('e1', new.env(parent=emptyenv())) assign('e2', new.env(parent=emptyenv()), envir=e1) assign('a', 3, envir=get('e2', envir=e1)) e1%:%e2%:%a `%:%<-`=function(x,y,z) { if(!is.environment(x)) { x=as.character(substitute(x)) x=get(x) } y=as.character(substitute(y)) assign(y, z, envir=x) } e1%:%e2%:%a=2#fails here -- Tom
Peter Dalgaard
2010-Jul-14 08:35 UTC
[R] How to define a function (with '<-') that has two arguments?
thmsfuller066 at gmail.com wrote:> Hi All, > > The last line if the following code returns the error right below this > paragraph. Essentially, I use the operator %:% to retrieve a variable > in a nested frame. Then I want to use the same operator (with '<-') to > change its value. I'm not sure if this is possible in R with %:% > operator. Would you please help and let me how to correct the code if > it is possible? > > Error in `%:%<-`(`*tmp*`, a, value = 2) : unused argument(s) (value = 2)I'm fairly sure I'd not want to take that route, but the direct cause of the error is that the value argument to an assignment function must be called "value", not "z". Beware though, that the code may still not do what you think it should do (e.g., the return value for an assignment function REPLACES the first argument). -- Peter Dalgaard Center for Statistics, Copenhagen Business School Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com
Hadley Wickham
2010-Jul-14 09:29 UTC
[R] How to define a function (with '<-') that has two arguments?
On Wed, Jul 14, 2010 at 7:39 AM, thmsfuller066 at gmail.com <thmsfuller066 at gmail.com> wrote:> Hi All, > > The last line if the following code returns the error right below this > paragraph. Essentially, I use the operator %:% to retrieve a variable > in a nested frame. Then I want to use the same operator (with '<-') to > change its value. ?I'm not sure if this is possible in R with %:% > operator. Would you please help and let me how to correct the code if > it is possible?Why not just do e1$e2$a <- 3 ? If you are trying to do something more complicated, it may help to understand that e1 %:% e2 %:% a <- 3 is effectively the same as tmp1 <- e1 %:% e2 tmp1 %:% a <- 3 tmp2 <- e1 tmp2 %:% e2 <- tmp1 e1 <- tmp2 so your %:%<- function is going to get called multiple times. Hadley -- Assistant Professor / Dobelman Family Junior Chair Department of Statistics / Rice University http://had.co.nz/