[英]using a for loop to utilize spotifyr get_artist_audio_features function in R, skip errors in the loop
I downloaded my personal Spotify data from the Spotify website.我从 Spotify 网站下载了我的个人 Spotify 数据。 I converted these data from JSON to a regular R dataframe for further analysis.
我将这些数据从 JSON 转换为常规的 R dataframe 以供进一步分析。 This personal dataframe has 4 columns:
这个个人 dataframe 有 4 列:
Endtime artistName trackName Msplayed
However, Spotify has many variables coupled to songs from an artist, that you can only retrieve using the function get_artist_audio_features
from the spotifyr
package. I want to join these variables to my personal dataframe. The package allows data retrieval for only one artist at a time and it would be very time consuming to write a line of code for all 3000+ artists in my dataframe.然而,Spotify 有许多与艺术家歌曲相关的变量,您只能使用 function
get_artist_audio_features
从spotifyr
package 检索这些变量。我想将这些变量加入我的个人 dataframe。package 一次只允许检索一位艺术家的数据并且为我的 dataframe 中的所有 3000+ 艺术家编写一行代码将非常耗时。
I used a for loop
to try and collect the metadata for the artists:我使用
for loop
来尝试收集艺术家的元数据:
empty_list <- vector(mode = "list")
for(i in df$artistName){
empty_list[[i]] <- get_artist_audio_features(i)
}
My dataframe also has podcasts, for which non of this meta-data is available.我的 dataframe 也有播客,但没有可用的元数据。 When i try using the function on a podcast i get the error message:
当我尝试在播客上使用 function 时,我收到错误消息:
Error in get_artist_audio_features(i) :
No artist found with artist_id=''.
In addition: Warning messages:
1: Unknown or uninitialised column: `id`.
2: Unknown or uninitialised column: `name`.
When i use the for loop, it stops as soon as the first error (podcast) in the dataframe occurs.当我使用 for 循环时,它会在 dataframe 中的第一个错误(播客)发生时立即停止。 When i feed it a vector of only artists and no podcasts, it works perfectly.
当我给它提供一个只有艺术家而不是播客的向量时,它工作得很好。
I checked stack for possible answers (most notably: Skipping error in for-loop ) but i cant get the loop to work.我检查了堆栈以寻找可能的答案(最值得注意的是: Skipping error in for-loop )但我无法让循环工作。
My question: how can i use the function spotifyr::get_artist_audio_features
in a for loop
and skip the errors, storing the results in a list.我的问题:如何在
for loop
中使用 function spotifyr::get_artist_audio_features
并跳过错误,将结果存储在列表中。 Unfortunately, it is very difficult to post a reproducable example, since you need to active a developer account on spotify to use the spotifyr
package.不幸的是,发布可重现的示例非常困难,因为您需要在 spotify 上激活开发者帐户才能使用
spotifyr
package。
It looks like your issue is in artist_id = ''
, so try the below code to see if it helps get you started (since I don't have reproducible data, not sure if it will help).看来您的问题出在
artist_id = ''
中,因此请尝试以下代码,看看它是否有助于您入门(因为我没有可重现的数据,不确定是否有帮助)。 In this case it should just skip the podcasts, but I'm sure some more codesmithing will allow you to put relevant data in the given list position.在这种情况下,它应该只是跳过播客,但我相信更多的代码设计将允许您将相关数据放入给定列表 position 中。
for(i in df$artistName){
if(artist_id = ''){
empty_list[[i]] <- NA
} else {
empty_list[[i]] <- get_artist_audio_features(i)
}
}
You could also use a while
loop conditioning on an incremental i
to restart the loop, but I can't do that without the data.您也可以在增量
i
上使用while
循环条件来重新启动循环,但没有数据我无法这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.