'What does the %||% operator do in R? Percent Pipe Pipe Percent

I came across this code when reviewing the R plotly library:

modify_list <- function(x, y, ...) {
  modifyList(x %||% list(), y %||% list(), ...)
}

What does the "%||%" operator do? And is it specific to plotly?



Solution 1:[1]

That is the infix function (from the rlang or purrr packages) that makes it easy to replace NULL with default values. You can find the documentation here.

You can also search for documentation by typing either of these. To search for anything else, replace the %||% and you will be able to look that up as well.

> help("%||%")
> ?"%||%"

If you want to find it anywhere in any packages you have installed (whether or not they are loaded) you need help.search("\\%\\|\\|\\%", agrep=FALSE) (because % and | are special characters in regular expressions)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ben Bolker