Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the %||% operator (used in ggplot2) and where is it defined?

Tags:

plot

r

ggplot2

I am going through some of the ggplot2 code and I stumbled over (well actually the compiler stumbled over it) the %||% operator. I imagine it is some kind of an "logical-or" function, seemingly related to parameters, but somehow it is not defined in my environment. I have included all the dependencies that I think ggplot2 has (plyr,scales,reshape2,digest,MASS,gtable) with a library statement but it did not help.

?%||% and ??%||% from R-studio didn't help, neither did using the SO search and Google and even Bing. I actually think most search services just refuse to search for such things, which makes finding these operators really difficult.

Not really sure where to look now, short of bugging the package authors who have far better things to do with their time - especially these package authors.

So where is it, and where should/could I have looked to find it? I really want to be self-sufficient about these kind of things.

like image 991
Mike Wise Avatar asked Feb 09 '23 02:02

Mike Wise


1 Answers

Your initial approach was good, one additional trick would be to add backticks to your query:

R> ?`%||%`

Which brings up the help page for null-default from purrr that describes it as "This infix function makes it easy to replace NULLs with a default value"

In use:

R> 1 %||% 2
[1] 1
R> NULL %||% 2
[1] 2
like image 140
Stedy Avatar answered Feb 11 '23 10:02

Stedy