Re: [R] Defining binary indexing operators

2005-04-27 Thread Gabor Grothendieck
On 4/27/05, Ali - <[EMAIL PROTECTED]> wrote: 
> 
> 
> > > >
> > > > 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")
> > >
> 
> If I got it right, in the above example you provided '$' is defined as a
> method for a S3 class. How is it possible to do the same with a S4 class. 
> If
> this is not possible, what is the best way to define the '$' operator 
> whose
> first arguments is a S4 object and doesn't overwrite the global 
> definition?
> 
> The myclass example is defined as an S3 method. In the above its defined 
> as a function, not a method.

[[alternative HTML version deleted]]

__
R-help@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


Re: [R] Defining binary indexing operators

2005-04-27 Thread Ali -

> >
> > 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")
>
If I got it right, in the above example you provided '$' is defined as a 
method for a S3 class. How is it possible to do the same with a S4 class. If 
this is not possible, what is the best way to define the '$' operator whose 
first arguments is a S4 object and doesn't overwrite the global definition?

__
R-help@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


Re: [R] Defining binary indexing operators

2005-04-27 Thread Gabor Grothendieck
On 4/27/05, Ali - <[EMAIL PROTECTED]> 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")
> 
> what about this approach:
> 
> foo <- function(x, y) x+y
> assign("$", foo)
> 
> would this overwrite $ 

 Yes.
 and make R to forget its definitions in the global
environment?
 Yes.
 Your construct might still be used in a local environment.
 f <- function(x,y) {
 "$" <- function(x,y) x+y
 x$y
}
f(1,2) # 3
 # or
z <- local({ 
 "$" <- function(x,y) x+y
 x <- y <- 3
 x$y
})
z # 6
 works and outside of f and the local, $ works normally.

[[alternative HTML version deleted]]

__
R-help@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


Re: [R] Defining binary indexing operators

2005-04-27 Thread Tony Plate
Excuse me!  I misunderstood the question, and indeed, it is necessary be 
that complicated when you try to make x$y behave the same as foo(x,y), 
rather than foo(x,"y") (doing the former would be inadvisible, as I 
think someelse pointed out too.)

Tony Plate wrote:
It's not necessary to be that complicated, is it?  AFAIK, the '$' 
operator is treated specially by the parser so that its RHS is treated 
as a string, not a variable name.  Hence, a method for "$" can just take 
the indexing argument directly as given -- no need for any fancy 
language tricks (eval(), etc.)

 > x <- structure(3, class = "myclass")
 > y <- 5
 > foo <- function(x,y) paste(x, " indexed by '", y, "'", sep="")
 > foo(x, y)
[1] "3 indexed by '5'"
 > "$.myclass" <- foo
 > x$y
[1] "3 indexed by 'y'"
 >
The point of the above example is that foo(x,y) behaves differently from 
x$y even when both call the same function: foo(x,y) uses the value of 
the variable 'y', whereas x$y uses the string "y".  This is as desired 
for an indexing operator "$".

-- Tony Plate

Gabor Grothendieck wrote:
On 4/27/05, Ali - <[EMAIL PROTECTED]> 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]]
__
R-help@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@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@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


Re: [R] Defining binary indexing operators

2005-04-27 Thread Ali -

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

__
R-help@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


Re: [R] Defining binary indexing operators

2005-04-27 Thread Tony Plate
It's not necessary to be that complicated, is it?  AFAIK, the '$' 
operator is treated specially by the parser so that its RHS is treated 
as a string, not a variable name.  Hence, a method for "$" can just take 
the indexing argument directly as given -- no need for any fancy 
language tricks (eval(), etc.)

> x <- structure(3, class = "myclass")
> y <- 5
> foo <- function(x,y) paste(x, " indexed by '", y, "'", sep="")
> foo(x, y)
[1] "3 indexed by '5'"
> "$.myclass" <- foo
> x$y
[1] "3 indexed by 'y'"
>
The point of the above example is that foo(x,y) behaves differently from 
x$y even when both call the same function: foo(x,y) uses the value of 
the variable 'y', whereas x$y uses the string "y".  This is as desired 
for an indexing operator "$".

-- Tony Plate

Gabor Grothendieck wrote:
On 4/27/05, Ali - <[EMAIL PROTECTED]> 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]]
__
R-help@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@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


Re: [R] Defining binary indexing operators

2005-04-27 Thread Gabor Grothendieck
On 4/27/05, Ali - <[EMAIL PROTECTED]> 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]]

__
R-help@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


RE: [R] Defining binary indexing operators

2005-04-27 Thread Huntsinger, Reid
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Huntsinger, Reid
Sent: Wednesday, April 27, 2005 4:10 PM
To: 'Ali -'; r-help@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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Ali -
Sent: Wednesday, April 27, 2005 3:11 PM
To: r-help@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@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@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}}

__
R-help@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


RE: [R] Defining binary indexing operators

2005-04-27 Thread Huntsinger, Reid
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: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Ali -
Sent: Wednesday, April 27, 2005 3:11 PM
To: r-help@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@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@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] Defining binary indexing operators

2005-04-27 Thread Ali -
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@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