0

I've edited my question to be more relevant

It's only been less than a month since I started to learn R and I'm trying to use it to get rid of the tedious work related to Facebook (extracting comments) that we use for our reports.

Using Rfacebook package, i made this script where it extracts (1) the posts of the page for a given period, and (2) Comments on those posts. It worked well for the page I'm doing the report for, but when I tried it on other pages with posts that had zero comments, it reported an error.

Here's the script: Loading libraries

library(Rfacebook)
library(lubridate)
library(tibble)

Setting time period. Change time as you please.

current_date <-Sys.Date()
past30days<-current_date-30

Assigning a page. Edit this to the page you are monitoring*

brand<-'bpi'

Authenticating Facebook. Use your own

app_id <- "xxxxxxxxxxxxxxxx"
app_secret <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
token <- fbOAuth(app_id,app_secret,extended_permissions = FALSE,legacy_permissions = FALSE)

Extract all posts from a page

listofposts <- getPage(brand, token, n = 5000, since = past30days, until = current_date, feed = FALSE, reactions = FALSE, verbose=TRUE)
write.csv(listofposts,file = paste0('AsOf',current_date,brand,'Posts','.csv'))

Convert to a data frame

df<-as_tibble(listofposts)

Convert to a vector

postidvector<-df[["id"]] 

Get the number of posts in the period

n<-length(postidvector)

Produce all comments via loop

reactions<-vector("list",n)
for(i in 1:n){
  reactions[[i]]<-assign(paste(brand,'Comments', i, sep = ""), (getPost((postidvector[i]),token,comments=T,likes=F,n.likes=5000,n.comments=10000))) 
}

Extract all comments per post to CSV

for(j in 1:n){
  write.csv(reactions[[j]],file = paste0('AsOf',current_date,brand,'Comments' ,j, '.csv'))
}

Here's the error when exporting the comments to CSV when I tried it on the pages with posts that had ZERO comments:

Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE,  : 
  arguments imply differing number of rows: 1, 0

I tried it on a heavy traffic page, and it worked fine too. One post had 10,000 comments and it extracted just fine. :(

Thanks in advance! :D

Chellie05
  • 1
  • 2

1 Answers1

2

Pages can be restricted by age or location. You can´t use an App Access Token for those, because it does not include a user session so Facebook does not know if you are allowed to see the Page content. You will have to use a User Token or Page Token for those.

andyrandy
  • 72,880
  • 8
  • 113
  • 130