Below is a simple example of how a quote is used to dynamically rename a tibble column.
quoteExample = function() {
new_name = quo("new_name_value");
tibble(old_name=list(1,2,3)) %>%
rename( !! quo_name(new_name) := old_name)
}
quoteExample()
Result= tibble(new_name_value=list(1,2,3))
Below the same simple example except this time in a lamda.
{function ()
new_name = quo("new_name_value");
tibble(old_name=list(1,2,3)) %>%
rename( !! quo_name(new_name) := old_name)
} ()
Result= Error in is_quosure(quo) : object 'new_name' not found
Why do quotes fail in a lamda but not in a named function? Where does this difference come from? Am I doing something wrong?
EDIT: The example above has been solved by Akrun, but below is another example that fails although the suggested solution has been applied:
df = tibble(data=list(tibble(old_name= c(1,2,3))))
df %>%
mutate(data = map(data, (function(d){
new_name = quo("new_value")
d %>% rename( !! quo_name(new_name) := old_name)
})))
Result: Error in is_quosure(quo) : object 'new_name' not found
Is this failing because of another issue?