0

I am having trouble joining 2 tibbles within a function using the curly curly ({{}}) syntax for the key variable.

The following code runs fine:

tab_across <- function(data, variable) {
  
  master_tibble <- {{data}} %>% count({{variable}})
  
  filtered_tibble <- {{data}} %>%
    filter(l1_nar == "filter text") %>%
    count({{variable}})
  
  master_tibble <- master_tibble %>%
    left_join(filtered_tibble, by = "my_variable")
  
  print(master_tibble)
}

tab_across(my_data, my_variable)

However, if I change "my_variable" to either {{variable}} or "{{variable}}", the code fails, and I get the following two errors, respectively:

Error in standardise_join_by(by, x_names = x_names, y_names = y_names) : 
  object 'my_variable' not found 

and

Error: Join columns must be present in data.
x Problem with `{{variable}}`.

Any help with this would be greatly appreciated.

kwela12
  • 15
  • 3
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Apr 24 '21 at 04:33
  • The `by=` argument doens't seem to support `{{}}`. You'll need to convert a quosure/expression to a character value as done in the duplicate. Also you don't need `{{}}` around `data`. You only need that around column names, not the data object itself. – MrFlick Apr 24 '21 at 04:41
  • Thank you, @MrFlick! The solution in the duplicate worked for me. Also, I bookmarked the reproducible example page for later reference. – kwela12 Apr 24 '21 at 08:28

0 Answers0