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

I look at the code ggplot2, and I came across (well, actually, the compiler came across it) on %||%. I suppose this is some kind of “logical” or “function” that seems to be related to parameters, but somehow it is not defined in my environment. I included all the dependencies that I think ggplot2have ( plyr,scales,reshape2,digest,MASS,gtable) with the operator library, but that did not help.

?%||%and ??%||%from R-studio they didn’t help, nor did they use SO and Google search and even Bing. I really think that most search services simply refuse to look for such things, which makes it difficult to find these operators.

I’m not quite sure where to look now, without touching the authors of the packages, who have much more opportunities to do their time - especially these authors.

So where is he, and where should I / could find him? I really want to be self-sufficient in such things.

+4
source share
2 answers

Your initial approach was a good one, another trick would be to add backlinks to your query:

R> ?`%||%`

Which brings up the help page with a default value of zero from purrr , which describes it as "This infix function makes it easy to replace NULL with a default value"

Using:

R> 1 %||% 2
[1] 1
R> NULL %||% 2
[1] 2
+4
source

: https://github.com/hadley/ggplot2/blob/master/R/utilities.r#L10-L12

"%||%" <- function(a, b) {
  if (!is.null(a)) a else b
}

. Hadley.

+4

Source: https://habr.com/ru/post/1623604/


All Articles