Search code examples
rtidyverserlang

Using rlang Package to Parse Quoted Argument


I'm hoping to string-split a single argument into two arguments and use each in different sections of a function.

Is it possible to do this using quasiquotation (!!) or other rlang functions?

Thanks!

Data:

person <- tibble(id = 1, age = 20)
friends <- tibble(id = c(2, 3, 4, 5), age = c(48, 29, 20, 48))

(unfunctional) Function:

different_age_friends <- function(condition, person = person, friends = friends ) {

  person <- person
  friends <- friends

  condition <- str_split(condition, " ~ ", simplify = T)
  condition_statement <- condition[1]
  filter_statement <- condition[2]

  if(!!condition_statement) {
    different_age_friends <- friends %>%
      filter(!!filter_statement)
  }

  return(return_same_age_friends)
}

Call:

different_age_friends(condition = "age == 20 ~ age == 48")

Desired Output

id age
2  48
5  48

Solution

  • Use rlang::parse_expr to convert strings to expressions and eval to evaluate them. eval() allows you to provide context for the expression in its second argument, where we supply the person data frame to it. In case of filter, the context is already understood to be the dataframe on the left-hand side of the %>% pipe.

    Another difference in how we handle the two expressions is that filter() has an additional internal layer of quasiquoation. Since you already have an expression, you don't need it to be quoted again, so you would use !! to unquote it.

    different_age_friends <- function(condition, p = person, f = friends) 
    {
      stmts <- str_split(condition, " ~ ")[[1]] %>% map( rlang::parse_expr )
    
      if( eval(stmts[[1]], p) )         # Effectively: eval(age == 20, person)
        f %>% filter(!!stmts[[2]])      # Effectively: friends %>% filter(age == 48)
      else
        f
    }
    
    different_age_friends(condition = "age == 20 ~ age == 48")
    # # A tibble: 2 x 2
    #      id   age
    #   <dbl> <dbl>
    # 1     2    48
    # 2     5    48
    

    Minor note:

    • You didn't provide a value for different_age_friends when the condition is false. I made an assumption that in this case, the entire friends list is to be returned.