I'm having a list of 4 items in R from a spotify API request:
> lijstwijk
[[1]]
Response [https://api.spotify.com/v1/search?q=track%3APompeii%20artist%3ABastille&type=track&limit=1]
Date: 2020-06-16 20:14
Status: 200
Content-Type: application/json; charset=utf-8
Size: 2.78 kB
{
"tracks" : {
"href" : "https://api.spotify.com/v1/search?query=track%3APompeii+artist%3ABastille&type=track&offset=0&limit=1",
"items" : [ {
"album" : {
"album_type" : "album",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/7EQ0qTo7fWT7DPxmxtSYEc"
},
...
[[2]]
Response [https://api.spotify.com/v1/search?q=track%3AUnderdog%20artist%3AAlicia%20Keys&type=track&limit=1]
Date: 2020-06-16 20:14
Status: 200
Content-Type: application/json; charset=utf-8
Size: 3.84 kB
{
"tracks" : {
"href" : "https://api.spotify.com/v1/search?query=track%3AUnderdog+artist%3AAlicia+Keys&type=track&offset=0&limit=1",
"items" : [ {
"album" : {
"album_type" : "single",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/3DiDSECUqqY1AuBP8qtaIa"
},
...
[[3]]
Response [https://api.spotify.com/v1/search?q=track%3AThe%20Way%20It%20Is%20artist%3ABruce%20Hornsby%20&%20The%20Range=&type=track&limit=1]
Date: 2020-06-16 20:14
Status: 200
Content-Type: application/json; charset=utf-8
Size: 4.58 kB
{
"tracks" : {
"href" : "https://api.spotify.com/v1/search?query=track%3AThe+Way+It+Is+artist%3ABruce+Hornsby+&type=track&offset=0&limit=1",
"items" : [ {
"album" : {
"album_type" : "album",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/2iM28IgKg89v1o7BTQAVPo"
},
...
[[4]]
Response [https://api.spotify.com/v1/search?q=track%3ABefore%20You%20Go%20artist%3ALewis%20Capaldi&type=track&limit=1]
Date: 2020-06-16 20:14
Status: 200
Content-Type: application/json; charset=utf-8
Size: 3.76 kB
{
"tracks" : {
"href" : "https://api.spotify.com/v1/search?query=track%3ABefore+You+Go+artist%3ALewis+Capaldi&type=track&offset=0&limit=1",
"items" : [ {
"album" : {
"album_type" : "single",
"artists" : [ {
"external_urls" : {
"spotify" : "https://open.spotify.com/artist/4GNC7GD6oZMSxPGyXy4MNB"
},
I only need the item spotify:track uri from every response in the list. So for the first two results i did this by hand:
# item one #
> itemone <- lijstwijk[[1]]
> itemoneparsed <- content(itemone, as="parsed")
> uritstone <- itemoneparsed$tracks$items[[1]]$uri
> uritstone
[1] "spotify:track:3gbBpTdY8lnQwqxNCcf795"
#item two#
> itemtwo <- lijstwijk[[2]]
> itemtwoparsed <- content(itemtwo, as="parsed")
> uritsttwo <- itemtwoparsed$tracks$items[[1]]$uri
> uritsttwo
[1] "spotify:track:3FGiFUJRRp5RGikVrs6kig"
How to make a dataframe containing the item spotify:track: from each response in the list?
How can i make one dataframe containing the item spotify:track: from each response of the list? As example:
Spotifytrackuri
1. "spotify:track:3gbBpTdY8lnQwqxNCcf795"
2. "spotify:track:3FGiFUJRRp5RGikVrs6kig"
3. "spotify:track:xxxxxxxxxxxxxxxxxxxxxx"
4. "spotify:track:xxxxxxxxxxxxxxxxxxxxxx"
Please note that for this example i have 4 responses. But there will be more. For each response , therefore, the item spotify: track: must be removed from the response and placed in one data frame.
How can i archieve this result? I've tried the lapply function but i'm getting a subscript out of bound error:
> lijstwijkuri <- lapply(lijstwijk, function(item) content(item, as="parsed")$tracks$items[[1]]$uri)
Error in content(item, as = "parsed")$tracks$items[[1]] :
subscript out of bounds