1

Just wondering what the current start of the art is for using tidyeval principles inside a function.

Imagine we have two tibbles in our environment, and we want to pass their names to a purrr::map call:

library(tidyverse)

t1 <- tribble(
  ~name,
  "foo"
  )

t2 <- t1

To print t1 and t2 via references to their names as strings, this works:

ls() %>% 
  map(
    ~get(.x)
    )

ut my attempt at more idiomatically tidyeval approaches don't

ls() %>% 
  map(
    ~{{.x}}
  )

ls() %>% 
  map(
    ~enquo(.x)
  )


ls() %>% 
  map(
    ~sym(.x)
  )

These all print the objects' names, not their contents.

How does this working using the tidyeval approach?

1
  • In this case ls() |> mget() is probably more direct. Not sure what trying to use rlang really adds to this type of problem. Commented Feb 23, 2023 at 21:02

1 Answer 1

2

you need to use eval() to evaluate the symbols

ls() %>% 
    map(
        ~eval(sym(.x))
    )

Note this way only works with sym(). so depending on what you want to do you might have to find a way to evaluate the other captured expressions.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.