6

我正在使用map()以下代码从 Facebook 获取发布数据:

posts_data <- map(posts$query_id, getPost, token = fb_oauth, n = 1000)

但是,有些query_id观察结果不正确,或者是共享事件,API 无法检索这些事件并给我一个错误,例如:

Error in callAPI(url = url, token = token, api = api) : 
  Unsupported get request. Object with ID '1816137521765810_1832190963493790' does not exist, cannot be loaded due to missing permissions, or does not support this operation. Please read the Graph API documentation at https://developers.facebook.com/docs/graph-api

我知道我可以possibly()在为这些错误返回输出的同时继续进行调用,这样函数就不会停止。但我不知道如何一起使用possibly()and map(),因为 possible() 只接受一个函数作为参数,并且不允许我向该函数传递额外的参数。

4

2 回答 2

5

possibly接受一个函数作为参数,但它返回另一个函数,该函数接受与其输入相同的参数。所以你应该能够做到:

posts_data <- map(posts$query_id, 
      possibly(getPost, otherwise = NA_character_), 
      token = fb_oauth, n = 1000)
于 2018-05-23T10:57:29.187 回答
1

我假设您寻求提取“评论”和“回复”等我与之前的答案略有不同 - 它转换为一个整洁的数据框(请注意 dplyr 和 plyr 之间的冲突)

1 提取您的帖子数据框(您已经完成了)

2 子集“评论”> 0 的帖子

sum(OB1_posts$comments_count)
mydata <- OB1_posts[OB1_posts$comments_count > 0,]
sum(mydata$comments_count) # How many 'Posts' had Comments

3 提取评论

3.1:创建可能()函数来捕获错误并忽略

library(purrr)
BruteForce_comments <- possibly( getPost, otherwise = NA_real_) 

Comments <- OB1_posts$id %>%
map(BruteForce_comments, token = fboauth, n = 200000, comments = TRUE,
likes = FALSE, n.likes=1, n.comments=600000) %>%
reduce(append)

转换为数据框

library(plyr)
OB1_Comments <- ldply(Comments, data.frame)

回复也是如此,然后将它们合并在一起(但您只需首先“简化”列配置)

如果您有任何其他问题,请pm我。该软件包非常出色,您可以从中获得大量信息-即使在 1 月下旬进行了更改之后

于 2018-05-30T10:11:54.107 回答