Evaluating a variable's string value as it's own variable

I'm looking to set a string variable at the start of a script that I then use to filter a data set with. For the example below, I want to use distinct() to pick rows with distinct cyl. When I run distinct(cyl, .keep_all =T) in the snippet below, I get the right result. But if I want to change that variable easily (like switching the distinct field to mpg), the code below fails. Any advice on how to successfully evaluate the key_distinct_field field in the code below so that it pulls the distinct cyl rows?

mtcars_tibble <- tibble(mtcars)
key_distinct_field <- "cyl"

mtcars_tibble |> 
  distinct(key_distinct_field, .keep_all =T)
mtcars_tibble |> 
  distinct(eval(parse(text=key_distinct_field)), .keep_all =T)

But perhaps someone who knows more than I do can explain why this works.

The problem is that dplyr (and other tidyverse packages) tend to expect arguments to be variable / column names. So distinct is looking for a column in mtcars named "key_distinct_field" (and of course not finding it).

The following works:
mtcars_tibble |> distinct(get(key_distinct_field), .keep_all =T)

2 Likes

@prubin Thanks so much! I had tried eval() and all_of(). The get() solution is exactly what I was looking for.

@prubin Just realized the get() function used in your solution creates a new column titled get(key_distinct_field) in the date frame. I can de-select the new column, but ideally would want to avoid having that new column generated all together

It's not entirely clear what the mission here is. The following produces a tibble with a single column named "key" containing the distinct value of the chosen field:
mtcars_tibble |> distinct("key" = get(key_distinct_field), .keep_all = F)
resulting in

# A tibble: 3 × 1
    key
  <dbl>
1     6
2     4
3     8

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.