Are "." and ".x" always equivalent when using a one argument function in a purrr:map() formula shortcut?

According to the documentation for purrr:map(), when using a formula shortcut you can refer to "." for one function arguments (though Hadley mentions it's not recommended in Section 9.2.2 of Advanced R (https://adv-r.hadley.nz/functionals.html)). In my experience, using a ".x" in place of a "." seems to work the same, as in these two lines of code:

# Using "."
map_dbl(mtcars, ~ length(unique(.)))

# Using ".x" gives the same output as above
map_dbl(mtcars, ~ length(unique(.x)))

Can you always expect "." and ".x" to work equivalently when referring to a single argument function within map()? Or are there special situations where you would need to use "." instead of ".x"?

Thanks!

Yes. The main reason to avoid . is that inside a pipe it can be confusing whether the . is handled by magrittr or purrr.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.