[R] Creating unary operators

Berton Gunter gunter.berton at gene.com
Fri Jan 7 23:39:17 CET 2005


Robert:

V&R's S-PROGRAMMING is an indispensable reference for sophisticated R
programming issues like those you bring up. Also the John Chambers Green
book (Programming with Data ...)might be useful .

I would await definitive confirmation from the experts, but AFAIK you're
correct -- you can only overload the existing unary operators, not create
new ones as you can binary operators via "%whatever%".

Of course, PLUSONE(2) is not all that different from %PLUSONE%2; or you
could even "fake" a unary operator by
 
"%PLUSONE%"<-function(x,y)y+1

and then have your parser turn your unary operator into 1%PLUSONE%y whenever
the increment expression appeared in your original language.  So in terms of
"code readability," I'm not clear why you really need this feature.

Same for the indexing generics, although here something like

"%[]%"<-function(x,y)x[y]

Seems almost exactly like what you want to do. But "readability", like
beauty, is in the eyes of the beholder, I guess.

HTH

Cheers,

Bert Gunter

-----Original Message-----
From: r-help-bounces at stat.math.ethz.ch
[mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of McGehee, Robert
Sent: Friday, January 07, 2005 1:26 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Creating unary operators

Is it correct (by its lack of mention in the R-Language Definition
Manual) that it is impossible to create a user-defined unary operator?

Ex: (This doesn't work, but it's an example of what I'm looking for)

> "%PLUSONE%" <- function(x) x + 1
> %PLUSONE% 2
[1] 3

And if the above is impossible, am I limited to only the + - ~ ! unary
operators for overloading?

On the same vein, is it correct that "[" and "[[" are the only possible
indexing generics? (Assigning "[" to "[[[" doesn't seem to work, and
nothing else is mentioned in the manual)

My goal is to parse well-defined character strings into R-readable code,
while preserving the structure of this text-language as much as
possible, so I'd rather compute on the R language rather than on this
text language.

Best,
Robert

Robert McGehee
Geode Capital Management, LLC
53 State Street, 5th Floor | Boston, MA | 02109
Tel: 617/392-8396    Fax:617/476-6389
mailto:robert.mcgehee at geodecapital.com



This e-mail, and any attachments hereto, are intended for use by the
addressee(s) only and may contain information that is (i) confidential
information of Geode Capital Management, LLC and/or its affiliates,
and/or (ii) proprietary information of Geode Capital Management, LLC
and/or its affiliates. If you are not the intended recipient of this
e-mail, or if you have otherwise received this e-mail in error, please
immediately notify me by telephone (you may call collect), or by e-mail,
and please permanently delete the original, any print outs and any
copies of the foregoing. Any dissemination, distribution or copying of
this e-mail is strictly prohibited.

______________________________________________
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




More information about the R-help mailing list