5

What's the most straight forward way of overloading '+' for characters? I have defined '%+%' <- function(...) paste(...,sep=""):

str <- "aa"%+%"bb"%+%"cc" #str="aabbcc"

But I don't like the syntax. I think str <- "aa"+"bb"+"cc" would be nicer.

(I am building long SQL queries to use with RODBC, the usual paste is not very handy in such situations. Any suggestions?)

teucer
  • 6,060
  • 2
  • 26
  • 36
  • Does R not support operator overloading? – David Heffernan Mar 16 '11 at 09:08
  • 2
    @David Heffernan It does. But does not allow to redefine some object (functions, operators, constants). [Check another question about it on stackoverflow](http://stackoverflow.com/questions/1319698/why-doesnt-operate-on-characters-in-r). – Marek Mar 16 '11 at 16:21

4 Answers4

11

You may try something like that :

R> oldplus <- `+`
R> `+` <- function(e1, e2) { 
R>     if (is.character(e1) && is.character(e2)) { 
R>          paste(e1,e2,sep="") 
R>      }
R>      else { 
R>          oldplus(e1,e2) 
R>      } 
R>  }

Which gives :

R> 2+3
[1] 5
R> "aa"+"bb"
[1] "aabb"

But as Sacha pointed out, overloading such a basic function is very dangerous, and I can't assure you it will not break your R session and make your computer explode :-)

juba
  • 47,631
  • 14
  • 113
  • 118
5

I think that using two arguments is better than the dots:

'%+%' <- function(x,y) paste(x,y,sep="")

"a"%+%"b"%+%"C"
[1] "abC"

If you really really want to you can overwrite +, but be veeeeery careful when doing this as you will break one of the most important functions in R. I can't think of any reason why you would want to do that over %+%:

# '+' <- function(x,y) paste(x,y,sep="")
# "a"+"b"+"C"
# [1] "abC"

rm('+')

commented it out to be sure I don't accidently break someones R:)

Sacha Epskamp
  • 46,463
  • 20
  • 113
  • 131
  • 1
    The proper way to do this is to define the operator on a particular class like `+.SQL` then to define your vector as having that class. That way you can't break the function for everyone else. See http://stackoverflow.com/a/4731547/245022 – adamleerich Jul 23 '12 at 14:15
  • If you're concerend about safety, wouldn't it be better to limit the overload on a string class? like `'+'.string <- function(` – JonnyRobbie Oct 04 '19 at 22:10
2

Why is the usual 'paste' not very handy? It's what it's meant for. Suggestions:

Write yourself an unusual paste function that does what you want. Maybe you just don't like typing 'sep=""' all the time. So write a function that calls paste with sep="". Or whatever.

Building long SQL queries with string concatenation is potential fail anyway. See http://xkcd.com/327/ for the canonical example.

Another possibility is some kind of templating solution. I've used the brew package in the past and it's great for that.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
  • 3
    `So write a function that calls paste with sep="".` But that's what `paste0()` is for! – Gaffi Mar 14 '18 at 15:07
0

You can find this operator in stringi package.

http://docs.rexamine.com/R-man/stringi/oper_plus.html

bartektartanus
  • 15,284
  • 6
  • 74
  • 102