I would like to use purrr's short-hand formula specification with multi-line formulas.
For example, I would like to repurpose the following formula:
purrr::imap_chr(sample(10), ~ paste0(.y, ": ", .x))
And do the following instead:
purrr::imap_chr(sample(10), ~
b <- 10
paste0(.y, ": ", .x + b))
However, I receive an error when I attempt to do this:
Error: unexpected symbol in: " b <- 10 paste0"
We can use braces { to create multi-line formulas
purrr::imap_chr(sample(10), ~ {
b <- 10
paste0(.y, ": ", .x + b)
})
#[1] "1: 18" "2: 16" "3: 19" "4: 11" "5: 13" "6: 12" "7: 15" "8: 14" "9: 17" "10: 20"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With