Learn R Programming

MazamaCoreUtils (version 0.6.2)

setIfNull: Set a variable to a default value if it is NULL

Description

Returns default when target is NULL; otherwise returns target unchanged.

Usage

setIfNull(target, default, enforcedType = NULL)

Value

The value of target if it is not NULL; otherwise default.

If enforcedType is specified, the returned value is coerced using the corresponding as.*() function.

Arguments

target

Object to test for NULL.

default

Object to return when target is NULL.

enforcedType

Optional character string specifying the suffix of an as.*() coercion function to apply to the returned value. For example, "double" uses as.double(), "character" uses as.character(), and "Date" uses as.Date().

If NULL (the default), no coercion is performed.

Details

This is useful for assigning default values to optional arguments while preserving any user-supplied value exactly as provided.

Optionally, enforcedType may be used to coerce the returned value to a specific type. This coercion is applied after the NULL check and affects both target and default.

Examples

Run this code
setIfNull(NULL, "foo")
setIfNull(10, 0)
setIfNull("15", 0)

# User-supplied values are returned unchanged
setIfNull("15", 0)
setIfNull("mean", 0)
setIfNull(mean, 0)

# Optional type enforcement
setIfNull("15", 0, enforcedType = "double")
setIfNull(NULL, "15", enforcedType = "integer")

Run the code above in your browser using DataLab