【问题标题】:How to use map() with possibly()如何将 map() 与可能 () 一起使用
【发布时间】:2018-05-23 10:52:22
【问题描述】:

我正在使用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()map(),因为 possible() 只接受一个函数作为参数,并且不允许我向该函数传递额外的参数。

【问题讨论】:

    标签: r purrr rfacebook


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我假设你试图提取“cmets”和“replies”等 我与之前的答案略有不同 - 它转换为一个整洁的数据框(请注意 dplyr 和 plyr 之间的冲突)

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

      2 子集 'cmets' > 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)
      

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

      如果您有任何其他问题,请私信我。这个包非常棒,你可以从中获得大量信息——即使在一月下旬发生了变化

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-20
        • 2022-07-05
        • 2018-04-06
        • 2021-08-18
        • 1970-01-01
        • 1970-01-01
        • 2010-10-22
        • 2020-02-09
        相关资源
        最近更新 更多