[R] How to define a function (with '<-') that has two arguments?

thmsfuller066 at gmail.com thmsfuller066 at gmail.com
Wed Jul 14 07:39:25 CEST 2010


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



More information about the R-help mailing list