Analyze Facebook with R

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:

First we have to get an access_token for our connection. To get it go to https://developers.facebook.com/tools/explorer

As API version use “Unversioned” and then click on “Get Access Token”.access token facebook open graph r

The easiest way is to select all permissions.

access token dialog R facebook

Then click on “Get Access Token” again and a dialog will appear which asks you to agree that the token can get all the information you just selected.

facebook dialog R

Click on “Okay” and you can find your Access Token in the box.

 

R

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: We are using a temporary access token in this example. You can also you authentication with an app, but there seems to be a problem with the Rfacebook package. I will add it again when it is working again.

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. If we use “me” as the username argument, it will return our own profile info.


me <- getUsers("me",token=token)

Now we saved our own public information in the variable „me“

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, simplify = FALSE)

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 getUsers() function.

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

For example to get the relationship status of our friends.


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.

129 thoughts on “Analyze Facebook with R

  1. after getting access token i run the following commands
    token<-"xxxxxxx"
    me<-getUsers("me", token)
    my_friends <- getFriends(token, simplify = FALSE).
    but no output generated

    • Ok what is stored in the variable “me” after you run me<-getUsers("me", token) ?

      Did you check all boxes in the "Select Permission" box before you create the access token?

      Regards

    • No this is not right. In the variable me there should be your own profile information.
      What is the output from:
      getUsers(“me”, token)
      Just paste this line in the console and show me the output please.

    • Now you can go on like I described in my tutorial with my_friends <- getFriends(token, simplify = FALSE)
      and you should get your list of friends.

      Regards

    • You have to store the information. Please just follow the steps in my tutorial.
      Save it with
      me <- getUsers("me",token=token)
      And get friends with:
      my_friends <- getFriends(token, simplify = FALSE)

  2. getUsers(“me”, token)
    me <- getUsers("me",token=token)
    my_friends <- getFriends(token, simplify = FALSE)
    these were the commands which i used which must give my friend list but output wasn't generated.

  3. [1] id name username first_name
    [5] last_name gender locale category
    [9] likes picture birthday location
    [13] hometown relationship_status
    (or 0-length row.names)

    this is the output.

    • So there is everything right with the R code. It just says that you don´t have friends on Facebook. So the Facebook API did not return any friends.
      You also checked all boxes on the second tab of the “Select Permissions” dialog? Check all boxes in the tab “Friends Data Permissions”.

    • So you are logged in with your real Facebook account to get the token?
      Do you have Facebook friends on this account?
      The Facebook API does say that there are no friends on the account you created the Access Token with.

  4. Hi…on running this code “table(my_friends_info$location)”, I am getting a statement saying “table of extent 0 >”. Can you please tell me what I am doing wrong? I have checked all the boxes, I believe there is some problem when I am trying to run “my_friends_info <- getUsers(my_friends$id, token=token, private_info=TRUE)" coz in this case when I print the "my_friends_info" dataset, I get a table with only NA values. Please help.

  5. I am able to run all of these commands and produce output, however, when I run my_friends <- getFriends(token, simplify = FALSE), the output only includes 3 individuals. I have some 600 friends, so not entirely sure why it is only producing output for 3. I have created a new API and reran the script, but gave me the same 3 friends.

    • Hey JR,
      I heard of similar problems. And I´m not completely sure why the API sometimes just return a certain amound of friends. But I will take a closer look at this problem and try to find a solution.

      Regards

  6. Hi Julianhi, I am facing error “Error in callAPI(query, token) : (#12) username is deprecated for versions v2.0 and higher” and I am not finding where to put http//:localhost:1410/. Please help

  7. You mean to say permanent authentication is not working at this time? And still I am facing the same problem (#12)…..

  8. Hi! Do you know whether the permanent token is working or not yet? I’d like to know If whether I’m failing at something or it’s a package thing. Thanks!

    -Adolfo

    • Hey,
      did you refresh the page and clear you cache?
      I updated the tutorial to work with a temporary token.
      But I think there will be a solution to working with a permanent token as well.
      I will keep you informed when you follow me on twitter.

      Regards

  9. Pingback: Analyze Facebook with R | jingwang512

Leave a reply to Kajol Kardam Cancel reply