问题
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)
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
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.
回答1:
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)
回答2:
Im assuming you seek to extract 'comments' and 'replies' etc I have a slightly different way to the previous answer - which converts into a neat dataframe (just be cautious with the conflict between dplyr and plyr)
1 Extract your dataframe of posts (which you already have done)
2 Subset the Posts where 'comments' > 0
sum(OB1_posts$comments_count)
mydata <- OB1_posts[OB1_posts$comments_count > 0,]
sum(mydata$comments_count) # How many 'Posts' had Comments
3 Extract Comments
3.1: create the possibly() function to catch errors and ignore
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)
Convert to DataFrame
library(plyr)
OB1_Comments <- ldply(Comments, data.frame)
This is the same for replies and then you merge them together (but you just have to 'streamline' the column configuration first)
If you have any other questions pm me. This package is outstanding and you can get a massive amount of info out of it - even after the changes in late January
来源:https://stackoverflow.com/questions/50486527/how-to-use-map-with-possibly