A Note on missing()

[This article was first published on English Blog on Yihui Xie | 谢益辉, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

I had misunderstood the function missing() for several years. Originally I thought it only applies to an argument that does not have a default or user-specified value. For example, this is fairly easy to understand:

f = function(x) {
  missing(x)
}
f()  # should be TRUE

One day I was surprised to find that this also returned TRUE:

f = function(x = 1) {
  missing(x)
}
f()

What?! x does have a default value 1; why is it considered missing? Then I realized missing() really meant “argument/value not passed” (to the function call).

Below is a yet more surprising fact that I discovered:

f = function(x) {
  missing(x)
}
g = function(y) {
  f(y)
}
g()  # still returns TRUE

I was surprised because when g calls f(y), y does not exist, yet it still worked. It looks like we did pass y (whatever it really is) to f(), but f() sees nothing. Sounds like fun of lazy evaluation or something.

Anyway, I don’t recommend using missing(). It is fragile and you may break it unintentionally. Per its help page:

missing(x) is only reliable if x has not been altered since entering the function […]

What I often do is to set the defautl value to NA or NULL, and use is.na() / is.null() to test if the default value was explicitly changed by the user. Of course, this has a different meaning with missing(), but it is more robust. In particular, NA works better when the function is used in a vectorized call, e.g., mapply().

To leave a comment for the author, please follow the link and comment on their blog: English Blog on Yihui Xie | 谢益辉.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)