Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析在R中作为JSON返回的Youtube统计信息_Json_R_Youtube - Fatal编程技术网

解析在R中作为JSON返回的Youtube统计信息

解析在R中作为JSON返回的Youtube统计信息,json,r,youtube,Json,R,Youtube,我有一个获取youtube统计数据的函数问题,当视频没有所有字段时,它会失败。这是我的代码和api响应。修改我的代码有什么帮助吗?谢谢 youtube.key <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" getYoutube <- function(youtube.video) { youtube.url <- paste0("https://www.googleapis.com/youtube/v3/videos?id=",

我有一个获取youtube统计数据的函数问题,当视频没有所有字段时,它会失败。这是我的代码和api响应。修改我的代码有什么帮助吗?谢谢

youtube.key <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

getYoutube <- function(youtube.video) {
  youtube.url <- paste0("https://www.googleapis.com/youtube/v3/videos?id=", 
                    youtube.video, "&key=", 
                    youtube.key, 
                     "&fields=items(id,snippet(channelId,title,categoryId),statistics)&part=snippet,s    tatistics", sep="")
  youtube.json <- getURL(youtube.url);
  youtube.json <- rjson::fromJSON(youtube.json);
  if(as.numeric(length(youtube.json$items) > 0)) {
  stats <- youtube.json$items[[1]]$statistics;
  snippet <- youtube.json$items[[1]]$snippet;
  df <- data.frame("youtube" = youtube.video, 
                 "youtube.category" = snippet[["categoryId"]],
                 "youtube.views" = as.integer(stats[["viewCount"]]),
                 "youtube.likes" = as.integer(stats[["likeCount"]]),
                 "youtube.dislikes" = as.integer(stats[["dislikeCount"]]),
                 "youtube.comments" = as.integer(stats[["commentCount"]]))
 }
 return(df);
}

youtube.video <- c("kAkoJN0aI70", "qWTPRvr50jo")

df <- do.call(rbind, lapply(youtube.video, 
                        function(youtube.video) 
                          getYoutube(youtube.video)))

Do既没有提供可复制的示例,也没有提供有效的json文件,但我猜您正在寻找
Do.call(dplyr::rbind.fill,…)
(json只需要稍加调整)到底是什么失败了?其他人使用类似于帮助处理
NULL
返回值的函数,但我们无法在没有API键的情况下运行您的代码,因为您尚未从URL抓取中提取数据,并且我们不知道您使用的是哪个JSON包。我使用的是“rjson”,错误是:error in data.frame(youtube=youtube.video,youtube.category=snippet)[[“categoryId”]],:参数意味着不同的行数:1,0这将是
NULL
s。请尝试在我的评论中使用链接中的函数,如:
as.integer(stats[[“dislikeCount”]]%| |%0)
非常感谢。
[{
 "items": [
  {
   "id": "kAkoJN0aI70",
   "snippet": {
   "channelId": "UCzmcJRoEGvKEGjJvhBkMn4g",
   "title": "Aviones de Madera por Sergio Morera M.",
   "categoryId": "24"
   },
   "statistics": {
   "viewCount": "583",
   "likeCount": "2",
   "dislikeCount": "0",
   "favoriteCount": "0",
   "commentCount": "0"
   }
  }
 ]
},

{
 "items": [
  {
   "id": "qWTPRvr50jo",
   "snippet": {
   "channelId": "UC1Pp8vqtfjNOjHFTCCSQfPg",
   "title": "juguetes juguetes aviones avion disney dusty hopcopper bombero    firefighter planes disney",
   "categoryId": "22"
   },
   "statistics": {
   "viewCount": "982",
   "favoriteCount": "0",
   "commentCount": "0"
   }
  }
 ]
}]