The missing()
function in R is used to check if a value was specified as an argument to a given function or not.
The syntax for the missing()
function is shown below:
missing(x)
The missing()
function takes a single parameter value, x
, which represents a formal argument.
# 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")
my_function
, using the function()
function.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.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.my_function
and pass only b
as an argument to it.