如何使用map()with possible()

Ray*_*S. 5 r purrr rfacebook

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

posts_data <- map(posts$query_id, getPost, token = fb_oauth, n = 1000)
Run Code Online (Sandbox Code Playgroud)

但是,有些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
Run Code Online (Sandbox Code Playgroud)

我知道我可以possibly()在返回那些错误的输出时继续调用,这样函数就不会停止.但我不知道如何使用possibly()map()在一起,因为可能是()只需要一个函数作为参数,并且不允许我传递额外的参数给这个函数.

小智 6

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

posts_data <- map(posts$query_id, 
      possibly(getPost, otherwise = NA_character_), 
      token = fb_oauth, n = 1000)
Run Code Online (Sandbox Code Playgroud)