Analyze Facebook with R

Update: In the newer versions of the Facebook API (this post is based on version 2.8) a lot of functions have much higher restrictions. So there are basically two versions to connect to the API: Via an app or via a temporary token. Using the app has the advantage that it is quite comfortable to connect to the API once the app is created. But you won´t all the data available via this method. The connection via the temporary token will give you more data but the token is only valid for 2 hours. 

Hello everybody! Today I found something very cool: There is a R package for mining Facebook. For Twitter there are several, but this is the first one really working well with Facebook. So I wanted to test it and was amazed about how easy it works.

Preparations in R

First we need to install the packages devtools and Rfacebook from github as this is currently the most recent version.

Connecting to the Facebook API via App:

First we have to create an App on the Facebook platform. We will use this app to connect to the Facebook API. This way you can manage your connections very good but it also has some disadvantages. Different than in version 1 of the API you can now just get information of the friends who are also using the app. This creates big problems as you can´t create a friend network with all you friends anymore.

To create a new app go to https://developers.facebook.com

 

analyze facebook R

Click on “Apps” and choose “Add a New App“. In the next window choose “Website” and give your app a fancy name.

analyze facebook R

Apply the changes with “Create App ID“.

analyze facebook R

Welcome to your first own Facebook app!

Ok now we need to connect our R session with our test app and authenticate it to our Facebook Profile for Data Mining. Rfacebook offers a very easy function for that.

Just copy your app id and your app secret from your app settings on the Facebook developer page.

The console will then print you the message:

Facebook R message

Copy the URL and go to the settings of your Facebook app. Click on the settings tab on the left side and then choose “+ Add Platform“.

R Facebook settings tab

Then add the URL in the field “Site URL” and save the changes.

Facebook R add url

Go back to your R session and hit enter. Then a browser window should open you have to allow the app to access your Facebook account.

If everything worked the browser should show the message

Facebook R success

And your R console will confirm it with

Facebook R success console

You can then save your fb_oauth object and use it for the next time.

 

Analyze Facebook with R!

Now we connected everything and have access to Facebook. We will start with getting our own profile information.

As I mentioned before because of Facebook´s new API policies the information you can get is very limited compared to the amount you were able to download with apps using API 1.

So I will just show you now how to get your own personal information but other posts will follow with new use-cases of the new API version.

The getUsers function returns public information about one or more Facebook user. If we use “me” as the username argument, it will return our own profile info.

Now we saved our own public information in the variable „me“ and you can take a look at it.

An advantage of the new API version is that you can get more than 100 likes. You can get the things you liked with:

 

Connecting to the Facebook API via Authentication Token:

The connection via an manually created authentication token has some less restrictions and so you can get more data. However, this token is only valid for 2 hours.

You can create your token at https://developers.facebook.com/tools/explorer

 

Analyze Facebook R

Analyze Facebook with R!

You can see some more examples in the readme file of the Rfacebook package at GiHub: https://github.com/pablobarbera/Rfacebook

 

Questions?

If you have any questions feel free to ask me on Twitter or write a comment here.

Julian Hillebrand

During my time at university and learning about the basics of economics I started heavily exploring the possibilities and changes caused by digital disruptions and the process of digital transformation, whereby I focused on the importance of data and data analytics and combination with marketing and management.
My personal focus of interest lies heavily on technology, digital marketing and data analytics. I made early acquaintance with programming and digital technology and never stop being interested in following the newest innovations.

I am an open, communicative and curious person. I enjoy writing, blogging and speaking about technology.