r-lib / rlang

Low-level API for programming with R
https://rlang.r-lib.org
Other
491 stars 131 forks source link

rlang::qq_show return code is not same with excution #1640

Closed djhbs closed 11 months ago

djhbs commented 11 months ago

Hi, thank you very much for help.

In the below example, the rlang::qq_show show the right code:

tibble::tibble(a = character(0), b = integer(), c = double())

but the execution does not return right variable names, only the right type.

For first variable, the right name should be a not a = character(0)

var <- c("a = character(0)", "b = integer()", "c = double()")
rlang::qq_show(tibble::tibble(!!!rlang::parse_exprs(var)))
#> tibble::tibble(a = character(0), b = integer(), c = double())

tibble::tibble(a = character(0), b = integer(), c = double())
#> # A tibble: 0 × 3
#> # ℹ 3 variables: a <chr>, b <int>, c <dbl>

df <- tibble::tibble(!!!rlang::parse_exprs(var))
df
#> # A tibble: 0 × 3
#> # ℹ 3 variables: a = character(0) <chr>, b = integer() <int>,
#> #   c = double() <dbl>

I am not sure I use the function wrongly, but the result is strange.

lionel- commented 11 months ago

That's because a = character(0) is an assignment expression just like a <- character(0). And you are injecting this whole assignment as individual argument.

If you want to pass names, do it via the names of the list of expressions, e.g.

exprs <- c(a = "character(0)", b = "integer()", c = "double()")

# Or equivalently:
exprs <- list(a = quote(character(0)), b = quote(integer()), c = quote(double()))
djhbs commented 11 months ago

Thank you so much for your detailed explanation.