Extract -- the best kept secret from magrittr (well, at least for me!)

magrittr

#1

I can't believe I just found out about magrittr::extract() and magrittr::extract2(). Super cool (and pipeable) functions! They make my code look waaaaay nicer.

EX:

library(tidyverse)
library(magrittr)

mylist <- list("this", "is", "pretty", "cool")

mylist[[3]]
#> [1] "pretty"
mylist %>% magrittr::extract2(3)
#> [1] "pretty"

Created on 2018-05-27 by the reprex package (v0.2.0).


#2

If you like that, you'll get a real kick out of purrr::pluck :grin:

library(tidyverse)

mylist <- list("this", "is", "pretty", "cool")

mylist[[3]]
#> [1] "pretty"
mylist %>% pluck(3)
#> [1] "pretty"

pluck has a few more tricks up its sleeve... check the linked documentation!


#3

oh boy. that's amazing


#4

How did I not know that? Who knows. What I lost from embarrassment I gained in knowledge haha. Thanks!