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

Feature request: can data_syms() keep NULL elements as-is? #1765

Open
teunbrand opened this issue Dec 9, 2024 · 2 comments
Open

Feature request: can data_syms() keep NULL elements as-is? #1765

teunbrand opened this issue Dec 9, 2024 · 2 comments

Comments

@teunbrand
Copy link
Contributor

When passing variable names as string, is seems natural to have optional arguments default to NULL.
However, this makes it difficult to use data_syms(), as it cannot convert these.

library(rlang)

f <- function(df, a = NULL, b = NULL) {
  syms <- data_syms(list(a = a, b = b))
  vctrs::data_frame(!!!lapply(syms, eval_tidy, data = df))
}

mtcars |> f("cyl", "disp") |> head()
#>   a   b
#> 1 6 160
#> 2 6 160
#> 3 4 108
#> 4 6 258
#> 5 8 360
#> 6 6 225

# Here, we only want the `a` column
mtcars |> f("cyl") |> head()
#> Error in `sym()`:
#> ! Can't convert `NULL` to a symbol.

Created on 2024-12-09 with reprex v2.1.1

My proposal is to make it easy to have data_syms(list(NULL)) return list(NULL).
For context, this is related to tidyverse/ggplot2#6208.

@lionel-
Copy link
Member

lionel- commented Dec 9, 2024

hmm I don't know. It seems like it's better to be explicit at the call site by calling compact() before data_syms(), that will make it very clear what's going on and is still quite terse.

@teunbrand
Copy link
Contributor Author

Thanks for the response Lionel. I think we'd like to preserve NULL rather than filter out NULL-elements. From ggplot2's end, geom_point(mapping = aes(colour = NULL)) negates a global mapping in ggplot(mapping = aes(colour = foo)). I think this is useful behaviour to carry over.

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