Analyze Facebook with R

November 19, 2013
By

(This article was first published on julianhi's Blog » R Tutorials, and kindly contributed to R-bloggers)

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.

Setup:

The first thing we need is a Facebook app.
Visit the Facebook developer center, register as Developer and create your test app.
So visit https://developers.facebook.com/apps and click „register as developer“.

You can then create your new app:

df5753d952ab36320531efdda1d57bf4

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.

R

Authentication

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.

fbAuth

fbOAuth(app_id, app_secret, extended_permissions = TRUE)

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

 require("Rfacebook")
 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:

83cc56c80c26ecc29fa16a3263cd67b8

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.

d96e511e0922ba99c3fec5b516f171dd

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

0dcd43b71b6b94258329cd60e5d30a20

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:

5fcd62d2245a33c93e4085b0b8a5af1c

And that´s what we do.

075dcaace9d9b9f02558bbd11115405d

#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
 load("fb_oauth")

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:

0dc0e4ddfe91b839fa690314290bc3a4

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

 getUser()

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

table(my_friends_info$relationship_status)

d13beec3b80973ce409a8ebbb268d0c7

Questions?

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.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

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)