injection not get right variable name in tibble::tibble

Dear all, thank your time for help the below problem.

The rlang::qq_show show the rigth code tibble::tibble(a = character(0), b = integer(), c = double()), but the exctution does not returen right variable names but 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>

Created on 2023-07-25 with reprex v2.0.2

Interesting, if it causes a problem you could raise it as an issue on the github.

This similar pattern does work :

var2 <-  c("character(0)", "integer()", "double()")
names(var2) <- letters[1:3]

tibble::tibble(!!!rlang::parse_exprs(var2))
1 Like

thanks very much for you reply, the current ansewer work well for what I need.

But for why rlang::qq_show different from the excution, I am not sure that I use the function by mistake, I will try to raise a issue on github

anoter solution

var_name <- c("a","b","c")
var_type <- c("character(0)", "integer()", "double()")
var_type %>% purrr::map(~ eval(rlang::parse_expr(.x))) %>%
  dplyr::bind_cols()

in general a map followed by bind_cols can be replaced with map_dfc()

Thanks for suggestion

I had got the explanation from rlang developer. see #1640

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

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

Created on 2023-07-26 with reprex v2.0.2

appreciate the developer again

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.