Search code examples
rtidyverserlangtidyeval

Current way to use rlang and tidyeval inside functions


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?


Solution

  • 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.