I am trying to update my following code because funs( MY_FUN ) is soft depreciated. I know that the replacement for this should be list( ~MY_FUN ), but this doesnt seem to be working for my code.
Here are my data frames:
fake_data <- data.frame(var1 = rep("TEMP", times = 5),
var2 = rep("TEMP", times = 5),
var3 = c(1:5),
stringsAsFactors = FALSE)
lookup_sub <- data.frame(var_names = c("var1", "var2", "var3"),
example_value = c("a", "b", "c"),
stringsAsFactors = FALSE)
The following line of code works and does exactly what I want it to:
library(tidyverse)
library(rlang)
fake_data %>%
mutate_if(.predicate = rlang::as_function(function(x){identical("TEMP", unique(x))}),
.funs = funs(as.character((lookup_sub %>% filter(var_names == quo_name(quo(.))) %>% pull(example_value))[1])))
Resulting in
var1 var2 var3
1 a b 1
2 a b 2
3 a b 3
4 a b 4
5 a b 5
BUT using the not-depreciated argument gives all NA values to the rows that evaluate to TRUE in the predicate
fake_data %>%
mutate_if(.predicate = rlang::as_function(function(x){identical("TEMP", unique(x))}),
.funs = list(~as.character((lookup_sub %>% filter(var_names == quo_name(quo(.))) %>% pull(example_value))[1])))
which results in
var1 var2 var3
1 <NA> <NA> 1
2 <NA> <NA> 2
3 <NA> <NA> 3
4 <NA> <NA> 4
5 <NA> <NA> 5
Can anybody explain this to me? I know that the problem occurs because of quo_name(quo(.)) but I dont know how to fix it. Thank you!