R: Convenience functions for handling 'NULL' objects.

NVL {statnet.common} | R Documentation

## Convenience functions for handling `NULL` objects.

### Description

Convenience functions for handling `NULL` objects.

### Usage

    NVL(...)

    NVL2(test, notnull, null = NULL)

    NVL3(test, notnull, null = NULL)

    EVL(...)

    EVL2(test, notnull, null = NULL)

    EVL3(test, notnull, null = NULL)

    NVL(x) <- value

    EVL(x) <- value

### Arguments

`..., test` | expressions to be tested.
`notnull` | expression to be returned if `test` is not `NULL`.
`null` | expression to be returned if `test` is `NULL`.
`x` | an object to be overwritten if `NULL`.
`value` | new value for `x`.

### Functions

- `NVL`: Inspired by SQL function `NVL`, returns the first argument that is not `NULL`, or `NULL` if all arguments are `NULL`.

- `NVL2`: Inspired by Oracle SQL function `NVL2`, returns the second argument if the first argument is not `NULL` and the third argument if the first argument is `NULL`. The third argument defaults to `NULL`, so `NVL2(a, b)` can serve as shorthand for `(if(!is.null(a)) b)`.

- `NVL3`: Inspired by Oracle SQL `NVL2` function and `magittr` `%>%` operator, behaves as `NVL2` but `.`s in the second argument are substituted with the first argument.

- `EVL`: As `NVL`, but for any objects of length 0 (Empty) rather than just `NULL`. Note that if no non-zero-length arguments are given, `NULL` is returned.

- `EVL2`: As `NVL2`, but for any objects of length 0 (Empty) rather than just `NULL`.

- `EVL3`: As `NVL3`, but for any objects of length 0 (Empty) rather than just `NULL`.

- `NVL<-`: Assigning to `NVL` overwrites its first argument if that argument is `NULL`. Note that it will always return the right-hand-side of the assignment (`value`), regardless of what `x` is.

- `EVL<-`: As assignment to `NVL`, but for any objects of length 0 (Empty) rather than just `NULL`.

### Note

Whenever possible, these functions use lazy evaluation, so, for example `NVL(1, stop("Error!"))` will never evaluate the `stop` call and will not produce an error, whereas `NVL(NULL, stop("Error!"))` would.

### See Also

`NULL`, `is.null`, `if`

### Examples

    a <- NULL

    a # NULL
    NVL(a,0) # 0

    b <- 1

    b # 1
    NVL(b,0) # 1

    # Here, object x does not exist, but since b is not NULL, x is
    # never evaluated, so the statement finishes.
    NVL(b,x) # 1

    # Also,
    NVL(NULL,1,0) # 1
    NVL(NULL,0,1) # 0
    NVL(NULL,NULL,0) # 0
    NVL(NULL,NULL,NULL) # NULL

    NVL2(a, "not null!", "null!") # "null!"
    NVL2(b, "not null!", "null!") # "not null!"

    NVL3(a, "not null!", "null!") # "null!"
    NVL3(b, .+1, "null!") # 2

    NVL(NULL*2, 1) # numeric(0) is not NULL
    EVL(NULL*2, 1) # 1

    NVL(a) <- 2
    a # 2
    NVL(b) <- 2
    b # still 1

[Package statnet.common version 4.6.0 Index]