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 https://developers.facebook.com/apps 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.
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.
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:
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
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
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 load("fb_oauth")
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:
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 table(my_friends_info$relationship_status)
If you have any questions feel free to ask me on Twitter or write a comment here.