Hi All,
I am, as explained yesterday, in the process of learning purrr
.
So far, so very good, it is a wonderful tool.
But the thing that crosses me is the documentation, and notably when dealing with the pipes.
As soon as R has issued the native pipe, I switched from magrittr
to the R native pipe and replaced the old pipe by the official one.
But the thing with purr
's articles and, more generally, the code samples on this forum, Stackoverflow, etc. is that they still use the magrittr
pipe -- and for a good reason: they were written before the issuance of the native pipe.
But the parameter passing with the old pipe has become irrelevant to the native one and won't work any longer (notably: .$column
).
Moreover, R 4.10 has introduced the anonymous function notation \(x)
, which has added a new combinatorial obsolescence to the documentation...
IMO, the situation with the pipes has considerably increased the difficulty to learn purrr
for lack of a steadfast documentation.
Let's be more practical. I am trying to do the following:
- Load a tibble with two columns
- Pass both columns of the tibble to
purrr:walk2
and use the native pipe to do so.
For example, I would like to assign emojis each to their own global variable:
require(emoji)
theemo <- tibble::tribble(
~var, ~emo,
"blksqr", "black_large_square",
"blusqr", "blue_square",
"grnsqr", "green_square",
)
purrr::walk2(
.x = theemo$var,
.y = theemo$emo,
.f = ~ assign(x = .x, value = emoji::emoji(.y), envir = globalenv())
)
I have tried everything but was unable to pipe the emoji definition tibble to walk2
.
For example, this won't work (when it does in different contexts) :
require(emoji)
tibble::tribble(
~var, ~emo,
"blksqr", "black_large_square",
"blusqr", "blue_square",
"grnsqr", "green_square",
) |>
purrr::walk2(
.x = var,
.y = emo,
.f = ~ assign(x = .x, value = emoji::emoji(.y), envir = globalenv())
)
It is not a big deal but it would avoid creating a named variable (theemo
= the tibble) and would tighten my code.
Please help.
Thanks,
Stephen