Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q: calling functions in dplyr pipelines #171

Open
baumanno opened this issue Dec 10, 2021 · 3 comments
Open

Q: calling functions in dplyr pipelines #171

baumanno opened this issue Dec 10, 2021 · 3 comments

Comments

@baumanno
Copy link

baumanno commented Dec 10, 2021

Hi all, and many thanks for creating and maintining this package in a very welcoming spirit!
This is a question, not an issue per se, although I'd gladly send a PR to the Readme once this is figured out.

Is it possible to use functions exported by this package, e.g. search_spotify(), in dplyr pipelines?

I have a dataset containing triples of (track, artist, album), and I would like to load these into a tibble, then pipe each row to search_spotify and aggregate the result:

df <- tribble(
  ~track, ~artist, ~album,
  "Move This", "Technotronic", "Pump Up the Jam: The Album",
  "Amy's Theme", "Murray Gold", "Doctor Who: Series 5",
)

df %>% 
  search_spotify(q=sprintf("track:%s artist:%s album:%s", track, artist, album), type=c("track"))

Unfortunately, this throws me:

Error in sprintf("track:%s artist:%s album:%s", track, artist, album) : 
  object 'track' not found

I tried using the exposition pipe %$% to no avail:

df %$% 
  search_spotify(q=sprintf("track:%s artist:%s album:%s", track, artist, album), type=c("track"))

... which results in:

Error in vapply(elements, encode, character(1)) : 
  values must be length 1,
 but FUN(X[[1]]) result is length 5

I'm missing something, but I can't put my finger on what it is. Any clues?

Cheers!

Edit:
For sake of completeness, I'm using 2.2.3 from CRAN

@baumanno
Copy link
Author

Heya, digging around in the issues a bit I stumbled upon this resource: https://mirr.netlify.app/audio-features-spotify.html mentioned in #76, which works wonderfully:

df <- tribble(
  ~track, ~artist, ~album,
  "Move This", "Technotronic", "Pump Up the Jam: The Album",
  "Amy's Theme", "Murray Gold", "Doctor Who: Series 5",
)

track_spotify_id <- function(artist, track, album, type = c("track")) {
  search_results <- search_spotify(glue("artist:{artist} track:{track} album:{album}"), type = type)
  
  return(search_results$id[[1]])
}

possible_search <- possibly(track_spotify_id, otherwise = 0)

df %>%
  mutate(album = gsub('\\:.*', "", album)) %>% # if album contains a colon, take only the first substring up to it
  mutate(sid = pmap(list(artist, track, album), possible_search)) %>% 
  unnest(sid)

Some of my album-data is bogus, so I''ll have to implement a fallback search sans-album in track_spotify_id, but that's custom logic and hardly belongs here.

@antaldaniel
Copy link
Collaborator

Well, I think that we are talking about different issues here. If you want to do some programming that involves tibbles (data.frames) as opposed to vectors, we are not talking about a dplyr but a purrr pipeline, and instead of the mutate() function you would likelly use some version of map().

The other thing that you are mentioning is a kind of a type mismatch. The idea why we use vapply instaed of lapply or sapply is exactly because vapply throws an error if the result has an unexpect dimension or class, or some unexpected coercion takes place.

So if you can place here a reproducible error that gives a vapply error, I can look into it. If you want to use spotifyr in a tidy workflow, I think that you must work with purrr.

@antaldaniel antaldaniel reopened this Dec 10, 2021
@baumanno
Copy link
Author

Thanks for your reply!

Indeed, I understand now that purrr is required, the snippet above involves purrr::pmap.

As stated in the original post, I don't believe there is an error here, more of a confusion on my side as to how to join spotifyr and the tidyverse correctly, given the popularity of both.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants