Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FR: add env argument to is_missing() #1715

Open
DanChaltiel opened this issue May 30, 2024 · 3 comments
Open

FR: add env argument to is_missing() #1715

DanChaltiel opened this issue May 30, 2024 · 3 comments

Comments

@DanChaltiel
Copy link

DanChaltiel commented May 30, 2024

Hi,

In some cases, it can be interesting to know if an argument is missing from the caller frame.

For instance, I'd like to propose a standard way to handle arguments through options when they are missing. Here, being able to check the missingness from inside a helper would be very handy.

Here is a reprex with a clumsy workaround:

library(rlang)

is_miss_bad = function(x, env=parent.frame()){
  is_missing(x)
}
is_miss_good = function(x, env=parent.frame()){
  xname = caller_arg(x)
  eval(parse(text=paste0("missing(",xname,")")), envir=env)
}

f = function(a=2){
  cat("bad:", is_miss_bad(a), "\n")
  cat("good:", is_miss_good(a))
  invisible()
}

f()
#> bad: FALSE 
#> good: TRUE

Created on 2024-05-30 with reprex v2.1.0

Note that is_miss_bad() would actually work fine if a didn't have a default value.

Would you consider adding this feature to rlang::is_missing()?

@lionel-
Copy link
Member

lionel- commented May 30, 2024

I think I'd rather fix either is_missing() or missing() in base. This env argument doesn't seem ergonomic.

@DanChaltiel
Copy link
Author

DanChaltiel commented Jul 4, 2024

I edited my post to show the proper FR demand.
However, I am not sure to understand, what do you mean by "fix in base"?
Also, what would you suggest instead of env?
If you don't think this would be a nice improvement to rlang::is_missing(), would you have a suggestion on a better way to do that than eval(parse(...))?

@lionel-
Copy link
Member

lionel- commented Aug 6, 2024

The C API of R is about to gain an env_binding_type() function that will return an enum, with the missing argument being one of the possible kinds (see https://gist.github.com/lionel-/1ebcbd5ec69c0775d514c329522408a3 for more information).

So I think we could expose this on the R level, and you could use that to detect missing arguments in a caller frame. We could add individual variants too, such as:

env_has_missing <- function(env, nm) {
  env_binding_type(env, nm) == "missing"
}

But I'm not sure we want to add so many predicates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants