Skip to content
Back to formatted view

Raw Message

Message-ID: <BAY17-F35F3F8D7394300EB4E733BD1220@phx.gbl>
Date: 2005-04-27T22:57:23Z
From: Ali -
Subject: Defining binary indexing operators
In-Reply-To: <971536df0504271521785332a@mail.gmail.com>

> >
> > 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?