#' The usual nullish operator
#'
#' This operator returns `y` if and only if `x` is `NULL`. Otherwise, it
#' returns `x`. It is most useful to provide alternatives in assignment
#' operations. See examples.
#'
#' @name op-nullish
#'
#' @param x,y `[any]`
#'
#' \R Objects.
#'
#' @returns Either `x` or `y`. The latter is returned if and only if `x`
#' is `NULL`.
#'
#' @examples
#' ## Assign an object to a name, and provide an alternative if it is NULL.
#' a <- 1L
#' b <- 2L
#' c <- a %||% b
#'
#' identical(c, a) # TRUE
#'
#' a <- NULL
#' c <- a %||% b
#'
#' identical(c, b) # TRUE
#'
#' @export
`%||%` <- function(x, y)
{
return(if (is_nul(x)) y else x)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.