# This works with map_chhr() because str_to_upper is vectorizedupper_case_anon <-function(word) str_to_upper(word)upper_case_anon(words)
[1] "I" "LIKE" "TO" "EAT" "CHEESE" "!"
Does this work?
# This works because str_to_upper is vectorizedupper_case_anon <-function(word) str_to_upper(word)upper_case_anon(words)
[1] "I" "LIKE" "TO" "EAT" "CHEESE" "!"
Yes, but it can get confusing.
This does not work
library(tidyverse)confusing_function <-function(word) upper_case_words<-str_to_upper(word)## This should lower the word's case, but it doesn't because its not part of the function. str_to_lower(upper_case_words)confusing_function(words)
Last example fixed with
confusing_function <-function(word){ upper_case_words<-str_to_upper(word)## Now it is part of the function. str_to_lower(upper_case_words)}confusing_function(words)
[1] "i" "like" "to" "eat" "cheese" "!"
Summary
Use an anonymous function to do one thing.
If it get complicated give some {}
More anonymous?
Since R version 4.1, May 2021.
Just use a \(argument) operations
# This is also an anonymous function. \(word) str_to_upper(word)map_chr(.x = words,.f = \(word) str_to_upper(word))
Deep dive
There are actually lots of ways of making anonymous functions.