如何将 map() 与 possible() 一起使用 [英] How to use map() with possibly()

查看:75
本文介绍了如何将 map() 与 possible() 一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using map() to get post data from Facebook using the following code:

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

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

However, some of the query_id observations are incorrect, or are shared events, which the API cannot retrieve and gives me an error like:

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

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

I understand that I can use possibly() to continue to make calls while returning an output for those errors, so that the function does not stop. But I do not know how to use possibly() and map() together, since possibly() only takes a function as an argument, and doesn't allow me to pass additional arguments to that function.

推荐答案

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

possibly takes a function as an argument, but it returns another function, which accepts the same arguments as its input does. So you should just be able to do:

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

这篇关于如何将 map() 与 possible() 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆