How to detect empty quosure in rlang?

f <- function(x) enquo(x)

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

None of these works:

> is_empty(e)
[1] FALSE
> is_missing(e)
[1] FALSE
> is_false(e)
[1] FALSE
> is_quosure(e)
[1] TRUE
+4
source share
2 answers

You can use quo_is_missing(x)which is an alias for is_missing(quo_get_expr(x)).

+3
source

Studying the printing method for a class quosureassumes that it receives an “empty” attribute as follows:

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

Unfortunately, it is env_typenot exported, and no function calls env_type(ultimately, the header to function C rlang_is_reference)

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

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

Printing Method for quosure:

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

rlang, , , -, , , :

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

- , rlang:::is_reference identical.

+3

Source: https://habr.com/ru/post/1677927/


All Articles