Assume we have a function like: foo <- function(x, y) how is it possible to define a binary indexing operator, denoted by $, so that x$y functions the same as foo(x, y)
That sounds like a recipe for headaches. If you want to use "x$y" because you want a certain kind of "x" to act like a list with components for certain "y", then you probably want to make a class of objects (x) which have "x$y" implemented as foo(x,y). That way you won't break existing code. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ali - Sent: Wednesday, April 27, 2005 3:11 PM To: r-help at stat.math.ethz.ch Subject: [R] Defining binary indexing operators Assume we have a function like: foo <- function(x, y) how is it possible to define a binary indexing operator, denoted by $, so that x$y functions the same as foo(x, y) ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
I should have added that if you're not wedded to "$" you can do $ "%f%" <- function(x,y) foo(x,y) for whatever name "f" you want, and then %f% is a binary infix operator form of foo(). Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Huntsinger, Reid Sent: Wednesday, April 27, 2005 4:10 PM To: 'Ali -'; r-help at stat.math.ethz.ch Subject: RE: [R] Defining binary indexing operators That sounds like a recipe for headaches. If you want to use "x$y" because you want a certain kind of "x" to act like a list with components for certain "y", then you probably want to make a class of objects (x) which have "x$y" implemented as foo(x,y). That way you won't break existing code. Reid Huntsinger -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Ali - Sent: Wednesday, April 27, 2005 3:11 PM To: r-help at stat.math.ethz.ch Subject: [R] Defining binary indexing operators Assume we have a function like: foo <- function(x, y) how is it possible to define a binary indexing operator, denoted by $, so that x$y functions the same as foo(x, y) ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html ______________________________________________ R-help at stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html ---------------------------------------------------------------------------- -- Notice: This e-mail message, together with any attachments,...{{dropped}}
On 4/27/05, Ali - <saveez@hotmail.com> wrote:> > Assume we have a function like: > > foo <- function(x, y) > > how is it possible to define a binary indexing operator, denoted by $, so > that > > x$y > > functions the same as > > foo(x, y)Here is an example. Note that $ does not evaluate y so you have to do it yourself: x <- structure(3, class = "myclass") y <- 5 foo <- function(x,y) x+y "$.myclass" <- function(x, i) { i <- eval.parent(parse(text=i)); foo(x, i) } x$y # structure(8, class = "myclass") [[alternative HTML version deleted]]
> > > > Assume we have a function like: > > > > foo <- function(x, y) > > > > how is it possible to define a binary indexing operator, denoted by $, >so > > that > > > > x$y > > > > functions the same as > > > > foo(x, y) > > Here is an example. Note that $ does not evaluate y so you have >to do it yourself: > >x <- structure(3, class = "myclass") >y <- 5 >foo <- function(x,y) x+y >"$.myclass" <- function(x, i) { i <- eval.parent(parse(text=i)); foo(x, i) >} >x$y # structure(8, class = "myclass")what about this approach: foo <- function(x, y) x+y assign("$", foo) would this overwrite $ and make R to forget its definitions in the global environment?