Get to use Facebook data via R

I’ve been building a Shiny app for work in the office. I was made to head a team that does some sort of web monitoring and we’ve decided to use R for some of the analyses. Before I attempt to detail some of the work done in the past couple of months or so, I will briefly share my attempt to access Facebook data just yesterday. The package to use is called Rfacebook, co-developed and maintained by Pablo Barbera.

First, open R. If you don’t have the package installed, run the code

install.packages("Rfacebook")

and then load it using library(Rfacebook)

To use the package, you need to first get a Facebook API access token – a temporary one that works for just 2 hours or one that you can use repeatedly for different sessions. To get the former, visit https://developers.facebook.com/tools/explorer/; for a permanent token, register an app with https://developers.facebook.com/ using your Facebook account, if you have one. The instructions in the documentation are actually crisp enough to guide you through the process; to access it, run help(fboauth).

I also found this blog, where the process is given in detail. Knowing Facebook’s penchant for changing things, don’t be surprised if there are slight modifications to the guidance you find here or elsewhere!

There are 3 things you want out of this – 1. App Version 2. App ID and 3. App Secret.

Now that you have your main details ready, you may want to store your token in your working directory so that you can call it up whenever you want to collect data through the API. The Rfacebook documentation proposed something like this

app_id <- 123XXXXXXX12345
app_secret <- ABDEFXXXXXXXXXXXXXXXXXXXX9876545
token <- fboauth(app_id, app_secret)
save(token, "my_token")
# Call up token later using load("my_token")

 

This last line with save() didn’t work for me and I can’t tell why. I even tried saving it as .RData file to no avail. This is what I did

saveRDS(token, "my_token.rds")

To use it, I would then use something like

token <- readRDS("my_token.rds)

 

Having done all, I could use different functions in the package. My only disappointment was that the main function that I had looked forward to using, searchFacebook(), has been deprecated for the current API version (at the time of writing this blog, v2.8) and Facebook had already retired the version (v1.0) for which this was possible.

So, I find the package only marginally useful for now, but will experiment a little and share if I find something interesting.

2 thoughts on “Get to use Facebook data via R

Comments