Analyze Facebook with R

[This article was first published on julianhi's Blog » R Tutorials, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Hello everybody!
Today I found something very cool: There is a R package for mining Facebook.
For Twitter there are different, 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.


The first thing we need is a Facebook app.
Visit the Facebook developer center, register as Developer and create your test app.
So visit and click „register as developer“.

You can then create your new app:


You can name it however you want as we won´t make this app available for all Facebook users.
And that´s it!
We created our Facebook App.

Bildschirmfoto 2013-11-19 um 22.21.38

Just keep this tab open and let´s look at R.



WARNING: There seems to be a problem with doing the authentication process in RStudio. A possible solution is to authenticate and create the oAuth token in a normal R session and then load it in RStudio to continue.

First we need to install the packages Rfacebook and Rook
It is important to write it exactly this way as there is an older package with nearly the same name, but it won´t work.

cd4ef1dbf77a43117054ba8865c4ace7 1185f3caaf97d7b975caf386c31c9582

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.
Note: I use an app for the authentication. You can also you OAuth tokens, but they will expire after 2 hours and you have to get a new one.


fbOAuth(app_id, app_secret, extended_permissions = TRUE)

So it just needs the app-id and the app-secret. But let´s try it:

 fb_oauth <- fbOAuth(app_id="123456789", app_secret="1A2B3C4D")

Of course you have to insert the app-id and the app-secret of your app you just created

Bildschirmfoto 2013-11-19 um 23.41.18

The function will return the following:


Here you can see how the Rfacebook package creates the connection to the Facebook API. It creates a listener on the port 1410.
And now we have to tell our app to send the data we want to this port.
Go to your app in the Facebook Developer center and go to Settings / Basic.


Then click on „Website with Facebook Login“ and enter http://localhost:1410/


Then save the changes and go back to your R session to hit any key to tell R that we can go on.

Your browser will then pop up and the app will ask for your permission

31bae8ef43f41955117d0da1e59dbc3f e58269bb0918ef2ebe1d6b20d4e5c0c5

Then wait a few seconds and the website will return:


And that´s what we do.


#now we have our fb_oauth connection
 #we will just save them to be able to use them later
 save(fb_oauth, file="fb_oauth“)

 #so if you want to connect to Facebook again you just have to call

Get Data!

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

 #the getUsers function return public information about one or more Facebook user
 me <- getUsers("me", token=fb_oauth)

Now we saved our own public information in the variable „me“
You can test it for example with:


For the case you forgot your name 😉

Let´s take a look at our friends.
Rfacebook offers the function

 getFriends(token, simplify = FALSE)

so you get your friends with:

 my_friends <- getFriends(token=fb_oauth, simplify=TRUE)

Your friends list is ordered by ID, so you can see who of your friends was the first on Facebook. Some very interesting inside.

You can show the friends which are the longest time on Facebook with:

 head(my_friends, n=10)

But let´s go a step further and get some deeper information about our friends.

We now have the variable my_friends.
We can combine it with the


function for example to get the relationship status of our friends.

 my_friends_info <- getUsers(my_friends$id, token=fb_oauth, private_info=TRUE)

 #create a table with the relationship statuses




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

To leave a comment for the author, please follow the link and comment on their blog: julianhi's Blog » R Tutorials. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)