[R] how about a "<p-" operator?

John Fox jfox at mcmaster.ca
Thu Mar 31 13:54:20 CEST 2011


Dear Ken and Bill,

Thanks for the comments. I obviously didn't think about the precedence issue
and was unaware that one could define :=.

Best,
 John

> -----Original Message-----
> From: Kenn Konstabel [mailto:lebatsnok at gmail.com]
> Sent: March-31-11 7:12 AM
> To: William Dunlap
> Cc: John Fox; Carl Witthoft; r-help at r-project.org
> Subject: Re: [R] how about a "<p-" operator?
> 
> In addition to %...% operators one can define := (I haven't seen this
> possibility documented anywhere but it's used in a package) which seems to
> have different precedence.
> 
> > `:=`<-`%<-%`   # the %<-% by John Fox
> > a
> [1] -1
> > a := 1000 + 1
> [1] 1001
> > a
> [1] 1001
> > a %<-% 1000 + 1
> [1] 1001
> > a
> [1] 1000
> 
> Regards,
> Kenn
> 
> Kenn Konstabel
> National Institute for Health Development Hiiu 42 Tallinn
> 
> On Thu, Mar 31, 2011 at 2:50 AM, William Dunlap <wdunlap at tibco.com> wrote:
> > The %...% operators are not a panacea.
> > they have the same precedence as `*`
> > and `/` (I think) so you get things like:
> >
> >  > x %<-% 10 - 8  # %<-% has higher precedence than -
> >  [1] 2
> >  > x # not what you thought it would be
> >  [1] 10
> >
> >  > x %<-% 10 ^3 # but lower than ^
> >  [1] 1000
> >  > x # this is what you expected
> >  [1] 1000
> >
> > It isn't that hard to write a package with your own parser in it.
> > Just have it generate the call tree from your input text and call
> > eval() on it.
> >
> > Bill Dunlap
> > Spotfire, TIBCO Software
> > wdunlap tibco.com
> >
> >> -----Original Message-----
> >> From: r-help-bounces at r-project.org
> >> [mailto:r-help-bounces at r-project.org] On Behalf Of John Fox
> >> Sent: Wednesday, March 30, 2011 4:34 PM
> >> To: 'Carl Witthoft'
> >> Cc: r-help at r-project.org
> >> Subject: Re: [R] how about a "<p-" operator?
> >>
> >> Dear Carl,
> >>
> >> I think that the following does what you want:
> >>
> >> > `%<-%` <- function(e1, e2){
> >> +   e1 <- deparse(substitute(e1))
> >> +   env <- parent.frame()
> >> +   assign(e1, e2, envir=env)
> >> +   e2
> >> + }
> >>
> >> > x %<-% 10
> >> [1] 10
> >>
> >> > x
> >> [1] 10
> >>
> >> But, as has been pointed out, it's probably easier just to
> >> parenthesize the usual assignment command.
> >>
> >> Regards,
> >>  John
> >>
> >> --------------------------------
> >> John Fox
> >> Senator William McMaster
> >>   Professor of Social Statistics
> >> Department of Sociology
> >> McMaster University
> >> Hamilton, Ontario, Canada
> >> http://socserv.mcmaster.ca/jfox
> >>
> >>
> >>
> >>
> >> > -----Original Message-----
> >> > From: r-help-bounces at r-project.org
> >> [mailto:r-help-bounces at r-project.org]
> >> > On Behalf Of Carl Witthoft
> >> > Sent: March-30-11 7:00 PM
> >> > To: r-help at r-project.org
> >> > Subject: [R] how about a "<p-" operator?
> >> >
> >> > I was cursing Matlab again today (what else is new) because
> >> the default
> >> > action for every Matlab command is to spew the result to
> >> the console, and
> >> > one must remember to put that darn ";"  at the end of every line.
> >> >
> >> > So I just wondered:  was there ever a discussion as to
> >> providing some
> >> > modified version of the "<-" and "->" operators in R to do
> >> the reverse?
> >> >   That is, since R does not print the values of a command
> >> to the console,
> >> > what if there were  an operator such that
> >> >
> >> >
> >> >   newobject <p- somefunction()
> >> >
> >> > would do the same as
> >> >
> >> > print(newobject <- somefunction())
> >> >
> >> >
> >> > Any thoughts?
> >> > Carl
> >> >
> >> > ______________________________________________
> >> > R-help at r-project.org mailing list
> >> > https://stat.ethz.ch/mailman/listinfo/r-help
> >> > PLEASE do read the posting guide http://www.R-project.org/posting-
> >> > guide.html and provide commented, minimal, self-contained,
> >> > reproducible code.
> >>
> >> ______________________________________________
> >> R-help at r-project.org mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> PLEASE do read the posting guide
> >> http://www.R-project.org/posting-guide.html
> >> and provide commented, minimal, self-contained, reproducible code.
> >>
> >
> > ______________________________________________
> > R-help at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >



More information about the R-help mailing list