Member-only story
How The %||% Operator in R 4.4 Keeps NULLs Out Of Your R Programming
Eliminate NULLS with the new built-in op-null-default function in R Programming Version 4.4
I wrote a post on the sort_by() function, a new built-in function in R programming version 4.4. Another function was added to R 4.4, one meant to address NULL values.
The 4.4 documentation on CRAN refers to the NULL-oriented function as an expressive idiom — That phrase is an admittedly awkward description.
For this function, I will call it the name used when it was first introduced in the rlang library — an op-null-default function. Now the same functionality of rlang’s op-null-default function is available as a built-in function. Here’s why it's useful.
How the op-null default works
The op-null-default operator is meant to provide a value R recognizes should it encounter a NULL value. It is an infix operator that sits between two values. It evaluates the value on the left and runs a conditional to use that value or use the value to the right of the operator.
In simpler terms, if x is NULL, the op-null-default operator will return the second value y. Otherwise, it will return x. Thus the op-null-default function is written as x %||% y and treated as a shorthand for a conditional statement.
Another way of stating it is “if x is not empty, use x; otherwise, use y.”