Is there a tidyverse alternative to magrittr::use_series?

Is there a tidyverse alternative to magrittr::use_series()? I don't mean pull() here, but something to access an attribute behind a $.

Like rpart$index would be rpart %>% magrittr::use_series(index)

Hi @eoppe1022! I'm not sure exactly what you mean by 'a tidyverse alternative' (since magrittr is already part of the tidyverse). What sort of problem are you having with use_series()?

1 Like

Absolutely nothing wrong with it. I was just talking to some people, and someone was wondering if there was already a use_series()-like function in dplyr or purrr. I wasn't sure, so I felt I'd ask my resident tidy experts :slight_smile:

1 Like

That's fair! :slight_smile: (Sorry if I came off a little brusque in my confusion!)

My first thought would be dplyr::pull() in lieu of magrittr::use_series(), since the latter's already an alias to $. I suppose there's also magrittr::extract() and magrittr::extract2(), but I think I'd be more inclined to use dplyr::pull() and dplyr::select()!

Thanks for the response! In this situation, pull() wouldn't work though. I mean something where $ isn't the "data" attribute but is something else. So like in my original question, randomForest$votes or lm$coef or rpart$index. You know what I mean? But yeah, I don't think there's anything out there

Ahh—you mean, something for extracting list elements outside of a data frame context? I think I'm following you better now :sweat_smile:

In that case, I think use_series() is probably the best option. There's also magrittr::extract(), which is an alias for [, and magrittr::extract2, which is an alias for [[, if you'd rather use a string to specify the list element. But I think that's exactly what those functions are for :slight_smile:

What you are looking for is purrr::pluck function

test_list <- list(index = 1:3, data = letters[1:3])
test_list %>% pluck("index")
#> [1] 1 2 3

Created on 2018-09-12 by the reprex package (v0.2.0).

About pluck

This is a generalised form of [[ which allows you to index deeply and flexibly into data structures. It supports R standard accessors like integer positions and string names, and also accepts arbitrary accessor functions, i.e. functions that take an object and return some internal piece.

See the help page for more example. (it is hidden from the reference page listing, I don't know why... :thinking: )


Man, my head is not switched on today. Pluck is great!


I even tried pluck() because I thought it would work, and I was surprised that it didn't when I did pluck(index).

Thanks again @cderv

1 Like