What is the missing() function in R?

Overview

The missing() function in R is used to check if a value was specified as an argument to a given function or not.

Syntax

The syntax for the missing() function is shown below:

missing(x)
Syntax for the missing() function in R

Parameters

The missing() function takes a single parameter value, x, which represents a formal argument.

Example

# creating a function
my_function <- function(a = 'default', b = 'default') {
# creting an if-else block
if (missing(a)) cat("Argument a is missing") else cat(paste("Argument a =", a));
cat ("\n");
if (missing(b)) cat("Argument b is missing") else cat(paste("Argument b =", b));
cat("\n\n");
}
my_function(b = "foo")

Explanation

  • Line 2: We create a function, my_function, using the function() function.
  • Line 4: We create an if-else block with the condition that if value a is missing as an argument to my_function, the string "Argument a is missing" should be returned. Otherwise, the value of a should be returned.
  • Line 6: We create another if-else block with the condition that if the value b is missing as an argument to my_function, the string "Argument b is missing" should be returned. Otherwise, the value of b should be returned.
  • Line 11: We call the function my_function and pass only b as an argument to it.