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”.
The easiest way is to select all permissions.
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.
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.
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)
Questions?
If you have any questions feel free to ask me on Twitter or write a comment here.
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
yes i have checked all of them except user_checkin
What is stored in the variable “me” after running me <- getUsers("me",token=token) ?
in token variable i am storing the access token which i got from Graph API Explorer.And that token is saved in me.
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.
Error: unexpected input in “getUsers(“”
is the output
Then the problem is that you use the wrong quote signs.
Use the straight ones. Then it should work.
yes now it worked for me THANK YOU VERY MUCH. now i am getting information about my profile. Now which command should be used.?
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
getUsers(“me”, token)
showed me information about my profile but now when i use the below command i get nothing,
my_friends <- getFriends(token, simplify = FALSE)
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)
> getUsers(“me”, token)
some output
> me my_friends
and then no output is generated…
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.
What is the output of
getFriends(token, simplify = FALSE)
Please paste this line and tell me the output.
[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”.
yes i have checked them all but nothing has changed. same output is coming.
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.
yes I am logged into my facebook account. Yes i have friends in this account. I don’t have any other account except this
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.
Hey,
could you please post the code you used? What is the output of the previous steps or what do the variables contain?
Regards
Hello, Can you please suggest me something about how I may get mutual friends using facebook api.
thank you
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
Hi Julianhi , even if I take unversioned api I am able to retrieve oly three friends out of 400 . Pls may I know the reason Julianhi
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
Hey,
please refresh the page. I updated the code a while ago and it seems like you used the old code.
Regards
You mean to say permanent authentication is not working at this time? And still I am facing the same problem (#12)…..
No I mean the update where I use the Open Graph API and manually getting a token instead of creating an App.
Regards
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
Hi, thanks for such a great posting! I’ve followed your detailed instruction, and it works fine! 🙂 looking forward to more great stuff from you.
Thank you very much 🙂
Regards
Pingback: Analyze Facebook with R | jingwang512