Skip to content

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

@teunbrand

Description

@teunbrand

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions