Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the %||% expression do in R

Tags:

r

I am working with some Code containing the expression %||%.

This is the context i encountered it:

# Example list for minimal example 
df <- list(a=1,2,c=3)
# code
ind <- names(df) %||% seq_along(df)

But R cannot find the operator %||%. I guess a package is not installed but i do not know which one. I also cannot find it on google or stackoverflow.

Could someone explain what it does and which package it belongs to?

like image 746
Sandwichnick Avatar asked May 19 '26 12:05

Sandwichnick


1 Answers

It's from ggplot2 utilities.

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

You can find in utilities.r

like image 96
Park Avatar answered May 21 '26 02:05

Park