Skip to contents

Like %||%, but also substitutes y when x has length zero (for example an empty array in a parsed JSON API response), not only when x is NULL. This matters when x feeds a single data.frame() column alongside scalar values: a length-zero x (as opposed to NULL) passes %||% unchanged and then throws "arguments imply differing number of rows" (in data.frame()) or "replacement has 0 rows" (in $<-), which typically aborts and silently drops the whole record it belongs to.

Usage

x %empty_or% y

Arguments

x, y

If x is NULL or has length 0, will return y; otherwise returns x.

Value

y if x is NULL or has length 0, otherwise x

Examples

1 %empty_or% 2
#> [1] 1
NULL %empty_or% 2
#> [1] 2
character(0) %empty_or% 2
#> [1] 2