% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stopIfNull.R
\name{stopIfNull}
\alias{stopIfNull}
\title{Stop if an object is \code{NULL}}
\usage{
stopIfNull(target, msg = NULL)
}
\arguments{
\item{target}{Object to test.}

\item{msg}{Optional error message to display if \code{target} is \code{NULL}.
Must be a character string of length one.}
}
\value{
Invisibly returns \code{target} when it is not \code{NULL}.
}
\description{
Convenience function for validating that an object is not \code{NULL}.
}
\details{
If \code{target} is not \code{NULL}, it is returned invisibly. If \code{target} is
\code{NULL}, the function stops with either a default or user-supplied
error message.

This function is especially useful for validating required function
arguments or for guarding intermediate results in pipelines.
}
\examples{
# Return input invisibly if not NULL
x <- stopIfNull(5)
print(x)

# Useful in pipelines
y <- 1:10
y_mean <-
  y \%>\%
  stopIfNull() \%>\%
  mean()

\dontrun{
# Trigger the default error message
testVar <- NULL
stopIfNull(testVar)

# Trigger a custom error message
stopIfNull(testVar, msg = "This is NULL")

# Make a failing pipeline
z <- NULL
z_mean <-
  z \%>\%
  stopIfNull("This has failed.") \%>\%
  mean()
}

}
