R: Specify a Default Value

orifnull {spatstat.utils} | R Documentation

## Specify a Default Value

### Description

Specify a value together with a default to be used when the first value is null.

### Usage

    a %orifnull% b

### Arguments

`a` | Any kind of object or expression to be evaluated.
`b` | Default value to be used when `a` is `NULL`. Any kind of object or expression to be evaluated.

### Details

The operator `%orifnull%` is designed to improve the readability of code.

`a %orifnull% b` is equivalent to `if(is.null(a)) a else b`.

That is, `a %orifnull% b` is equal to `a` provided `a` is not null, and otherwise the result is equal to `b`.

Expressions are evaluated only when necessary. If `a` is a language expression, it is first evaluated. Then if `is.null(a)` is `FALSE`, the result is `a`. Otherwise, `b` is evaluated, and the result is `b`. Note that `b` is not evaluated unless `a` is `NULL`.

The operator `%orifnull%` has higher precedence than the arithmetic operators `+`, `-`, `*`, `/` but lower precedence than `^`.

The operator is associative, and can be used repeatedly in an expression, so that a default value may have its own default. See the Examples.

### Value

The result is `a` if `a` is not `NULL`, and otherwise the result is `b`.

### Author(s)

Adrian Baddeley Adrian.Baddeley@curtin.edu.au, Rolf Turner r.turner@auckland.ac.nz and Ege Rubak rubak@math.aau.dk

### Examples

       x <- 7
       y <- 42
       z <- w <- NULL
       x %orifnull% y
       z %orifnull% y
       z %orifnull% x %orifnull% y
       z %orifnull% w %orifnull% y

[Package spatstat.utils version 2.3-1 Index]