R 4.30 Was Released
With R 4.3.0 released on Friday, you can now use an “underscore” with the built-in pipe, like you could use a “period” in maggittr pipe. While there are still some reasons to use maggittr, like T-pipes, assignment pipes and exposition pipes, I’ve never used them and they aren’t exported by default in the tidyverse.
For example, in maggittr you could do:
df %>% inner_join(states, .)
And now with the native pipe you can do the same thing:
df |> inner_join(states, _)
That was a major oversight when the created the native pipe, I’m not sure why it wasn’t originally implemented when 4.0.0 came out but it wasn’t.
Also, you can use _$value to extract something from R:
mtcars |> lm(mpg ~ disp, data = _) |> _$coef
Although, I’m not totally sure why you want to use a pipe like that when you can put the extractor directly on the lm:
mtcars |> lm(mpg ~ disp, data = _)$coef
Learn more about the changes in R 4.30: https://www.jumpingrivers.com/blog/whats-new-r43/
Get R version 4.3.0 (Already Tomorrow) which was released on 2023-04-21.
And here is the full list of changes in R 4.30.
2 Comments
I’ve been told not to use mybrilliantmodel$coef but rather to use the dedicated extractor function coef(mybrilliantmodel) … the reason for this is that if developers decide to completely change the structure of the output from lm or other modelling functions, they just need to update the coef(…) function to match, and the end-user shouldn’t notice anything’s changed. In practice it seems doubtful that anyone is going to mess with lm after all these years! But it’s still interesting advice in your case, since mybrilliantmodel |> coef() is much cleaner to read with a pipe than mybrilliantmodel |> _$coef and is arguably clearer than myamazingdata |> lm(winninglotterynumbers ~ deerpopulation, data = _)$coef
Thanks for sharing, that’s good to know.