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 functionmy_function <- function(a = 'default', b = 'default') {# creting an if-else blockif (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 thefunction()function. - Line 4: We create an if-else block with the condition that if value
ais missing as an argument tomy_function, the string"Argument a is missing"should be returned. Otherwise, the value ofashould be returned. - Line 6: We create another if-else block with the condition that if the value
bis missing as an argument tomy_function, the string"Argument b is missing"should be returned. Otherwise, the value ofbshould be returned. - Line 11: We call the function
my_functionand pass onlybas an argument to it.