简体   繁体   中英

How to detect an empty quosure in rlang?

f <- function(x) enquo(x)

e <- f()
#<quosure: empty>
#~

None of these work:

> is_empty(e)
[1] FALSE
> is_missing(e)
[1] FALSE
> is_false(e)
[1] FALSE
> is_quosure(e)
[1] TRUE

您可以使用quo_is_missing(x) ,它是is_missing(quo_get_expr(x))的别名。

Examining the print method for class quosure suggests it gets the "empty" attribute like so:

rlang:::env_type(get_env(e))
# [1] "empty"

Unfortunately, env_type is not exported, and neither are the functions env_type calls (ultimately heading to the C function rlang_is_reference )

You can get it more directly ( TRUE / FALSE ) as:

rlang:::is_reference(get_env(e), empty_env())
# [1] TRUE

Print method for quosure :

rlang:::print.quosure
# function (x, ...) 
# {
#     cat(paste0("<quosure: ", env_type(get_env(x)), ">\n"))
#     print(set_attrs(x, NULL))
#     invisible(x)
# }

I'm not familiar enough with rlang to state with certainty, but this appears to be a way to get what you want using exported functions:

identical(get_env(e), empty_env())
# [1] TRUE

Though I must be missing something, since rlang:::is_reference is not using identical .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM