Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Facebook Data Analytics using R
Dr. C. Naga RajuAssociate Professor & Head
Praveen Kumar DResearch Scholar
Department of Computer Science & EngineeringYSREC of Yogi Vemana University, Proddatur
Kadapa Dt., A. P, India
July 29, 2016Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 1 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
1 Introduction
2 Get permissions from facebook
3 Extract Facebook user details with R
4 Update Facebook status with R
5 Extract list of likes of a Facebook friend
6 Search a Group in Facebook with R
7 Search pages that mention a string
8 Extract Recent Posts from the Authenticated user’s newsfeed
9 Extract a post details
10 Extract List of Friends with their Information
11 Extract Facebook Picture of Authenticated User
12 Other Functions in Rfacebook Package
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 2 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Requirements
If we want to analyze the facebook data with R following arerequire
’Rfacebook’, ’RCrul’ packages in R language
Permissions to access facebook data
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 3 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Installation & Importing of Rfacebook
In R programming we install packages by usinginstall.packages(”Package Name”) function. i.einstall.packages(”Rfacebook”)install.packages(”RCrul”)
Load Rfacebook & RCrul packages in R using following coderequire(”Rfacebook”) or library(”Rfacebook”)require(”RCrul”) or library(”RCrul”)
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 4 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Graph API Explore
If we want to access the facebook data we must have thefacebook account.
For accessing facebook data we used one tool it is Graph APIExplore available in www.facebook.com with provided link:
https://developers.facebook.com/tools/explorer
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 5 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Graph API Explore Screen shot
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 6 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Obtaining Access Token
When someone connects with an app using Facebook Login, theapp will be able to obtain an access token which providestemporary, secure access to Facebook APIs.
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 7 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Get Permission from Facebook
We get permission from facebook by using Graph API Explore byClick on Get Token button, When we click on Get token followingscreen will display:
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 8 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Select the Required Permission from Facebook
Choose what kind of facebook data you need to access and clickon Get Access Token
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 9 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Check Authentication from Facebook
It will ask login you are not logged in. Other wise it ask fewoptions like change access permission of logged user and alsoshown what permissions it allow to analysis.
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 10 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Access Token from Graph API Tool
Finally it will generate Access token. Copy access token for furtheraccessing in R language.That access token valid only 2 hours.
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 11 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Get Permanent Access Token
We already discussed Access key is temporary and it is validup to 120 minutes.If we want a permanent access key we need to create anFacebook app and from there we get the access key.(https://developers.facebook.com/apps/) for more detailsHere already an YVU App is created with Uid and Secret key
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 12 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Get Permanent Access Token
From the above we get App ID and by clicking show buttonwe get App Secret Key.
Using fboAuth() method we store key permanently.
Example:fb oauth <- fbOAuth (aid=”1702xx”, a secret=”2f9xx”, permissions = T)
Storing and loading this in a file:save(fb oauth, file="fb oauth")
load("fb oauth")
Future we will use fb oauth as an access token
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 13 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
getUsers()
Definition
getUsers() retrieves public information about one or moreFacebook users and private information of the main User.
After version 2.0 of the Facebook API, only id, name, andpicture are available through the API. All the remaining fieldswill be missing.
Syntax: getUsers(users, token, private info = FALSE)
It extract information like User Id, name, username, first name,
middle name, last name, gender, locale, category, likes, picture
If we provide private info as TRUE, along with above detailsit provides birthday , Location, hometown, relationship status etc
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 14 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract Public Details of User From Facebook
For extracting facebook user details we require ‘Rfacebook’package and ‘Access Token’ We already get these.
Source coderequire("Rfacebook")
load("fb oauth")
me< −getUsers("me",token=
fb oauth )
me # printing output
OUTPUTId: 57142XXXXXname: PraveenKumar Dontausername: NAfirst name: PraveenKumarmiddle name: NAlast name: Dontagender : malelocale : en UScategory : NAlikes : NApicture : NA
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 15 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract Private Details of User From Facebook
Add private info is TRUE to get the Private details of the User.Source coderequire("Rfacebook")
load("fb oauth")
me< −getUsers("me",token=
fb oauth,private info=T)
me # printing output
OutputId: 57142XXXXXname: PraveenKumar Dontausername: NAfirst name: PraveenKumarmiddle name: NAlast name: Dontagender : malelocale : en UScategory : NAlikes : NApicture : NAbirthday : 04/21/1990Location : Cumbum, A. P, Indiahometown : Giddalurrelationship status : Single
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 16 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
updateStatus()
Definition
updateStatus() sends a status update that will be displayed on theFacebook profile of the authenticated user.
Syntax: updateStatus(text, token, link = NULL)
From above text is a string of message we want to poststatus. token means Access token
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 17 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Update Facebook status with R
Source coderequire("Rfacebook")
load("fb oauth")
text< −"Hi.. This post is
posted from R"
updateStatus(text,fb oauth, link
= NULL)
Output
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 18 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Update Facebook status along with Link withR
Source coderequire("Rfacebook")
load("fb oauth")
text< −"Hi.. This post is
posted from R"
link="https://www.youtube.com/
watch?v=Fa9gghVBlk4"
updateStatus(text,fb oauth,
link)
Output
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 19 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
getLikes()
Definition
getLikes() retrieves information about a friend’s likes. To retrievethe number of likes for a page
Syntax: getLikes(user, n , fb oauth )
From above user is any valid user id, fb oauth means Accesstoken and ’n’ indicates max number of like you want toretrieve.
This method gives output as "id" "names" "website"
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 20 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract list of likes of a Facebook friend
Source coderequire("Rfacebook")
load("fb oauth")
likes< −getLikes(‘me’, n = 500,
token= fb oauth )
table(likes)
Output
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 21 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
searchGroup()
Definition
Use searchGroup() in combination with getGroup to scrape publicposts on Facebook groups.
Syntax: searchGroup(name, token)
From above name is any valid String, token means Accesstoken
This method gives output as "name" "privacy" "id"
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 22 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract list of Groups with given string
Source coderequire("Rfacebook")
load("fb oauth")
name< −"india"
groups< −searchGroup(name,
fb oauth )
table(groups)
Output
Name Privacy IDIndian Job Seekers CLOSED 664407993570414INDIAN PAINT-INGS
OPEN 162815470570453
Jobs In India OPEN 655519397913849IEEE ComputerSociety - IndiaCouncil
CLOSED 338878412832563
Indian superstarprince maheshbabu fans
CLOSED 238349126246139
india OPEN 121233277959929
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 23 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
getGroup()
Definition
getGroup() retrieves information from a public Facebook group.
Syntax: getGroup(group id, token, n = 100, since =
NULL, until = NULL)
From above group id is any valid Id of a open group, tokenmeans Access token
This method gives output as from id","from name",
"message", "created time", , "type", "link", "id"
, "likes count", "comments count", "shares count"
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 24 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract list of Posts in a open Group
Source coderequire("Rfacebook")
load("fb oauth")
group id=655519397913849
groupdetails< −getGroup(group id
, token=fb oauth, n = 100, since
= NULL, until = NULL)
groupdetails
Outputfrom id: 1700931806812215from name: Swarna Cherukurimessage:http://tollywoodmonkeys.com/jet-airways-walkin-freshers-for-cabin-crew-from-dec-1-to-dec-12/created time: 2015-12-06T05:22:49+0000type: linklink:http://tollywoodmonkeys.com/jet-airways-walkin-freshers-for-cabin-crew-from-dec-1-to-dec-12/id: 655519397913849 743777472421374likes count: 20comments count: 13
shares count: 3
Complete output in a text file: ClickHere
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 25 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
searchPages()
Definition
searchPages()retrieves public pages that mention a given keyword
Syntax: searchPages(name, token , n)
From above name is any valid String, token means Accesstoken and n indicates number of maximum pages retrieve.
This method gives output as id, about, category,
description, general info, likes, link, city,
state, country, latitude, longitude, name,
talking about count, username , website
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 26 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Search pages & its information that mention astring
Source coderequire("Rfacebook")
load("fb oauth")
pages < − searchPages(
string="university", token=
fb oauth, n=500 )
pages
OutputId: 14483644XXXabout: Indian Space Research Organizationcategory: Govt. Org.description: Driven by inquisitiveness, Mankinds....general info:NA likes: 1117479link: https://www.facebook.com/ISRO/city: Bangalorestate: NAcountry: Indialatitude: 13.03825longitude:77.56492name: ISRO - Indian Space Research Organisationtalking about count: 6378username: ISRO
website: http://www.isro.gov.in
Complete output in a text file: ClickHere
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 27 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
getPage()
Definition
getPage() retrieves information from a public Facebook page. Notethat information about users that have turned on the ”follow”option on their profile can also be retrieved with this function
Syntax: getPage(page, fb oauth , n = 80, since =
NULL, until = NULL, feed = FALSE)
From above page is any valid Page id, fb oauth means Accesstoken and n indicates retrieve maximum number of post in apage .
This method gives output as "from id" "from name"
"message" "created time" "type" "link" "id"
"likes count" "comments count" "shares count"
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 28 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract list of posts from a public Facebookpage
Source coderequire("Rfacebook")
load("fb oauth")
page< −1448364408720250
pdetails < − getPage(page,
fb oauth, n = 10, since = NULL,
until = NULL, feed = FALSE)
pdetails # printing
Outputfrom id: 1448364408720250from name: ISRO - Indian Space Research Organisationmessage: A Kannada poem about the NAVICconstellati....created time: 2016-05-04T05:44:52+0000type: ”photo”link:https://www.facebook.com/ISRO/photos/a.1448404935382864.1073741828.1448364408720250/1727183020838386/?type=3id: 1448364408720250 1727183020838386
likes count: 672 comments count: 69 shares count: 124
Complete output in a text file: ClickHere
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 29 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
getNewsfeed()
Definition
getNewsfeed() retrieves status updates from the authenticateduser’s News Feed
Syntax: getNewsfeed(token, n)
From above token means Access token (Must be the extendedaccess token) and n indicates number of maximum postsretrieve.
This method gives output as "from id", "from name",
"to id", "to name", "message" , "created time",
"type", "link", "id" , "likes count",
"comments count", "shares count"
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 30 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract list of posts from a public Facebookpage
Source coderequire("Rfacebook")
load("fb oauth")
news < −getNewsfeed(token=fb oauth)
news # printing
Outputfrom id: 290897664260544from name: The BACK Benchersto id: NAto name: NAmessage: THAT DIFFERENCE...created time: 2016-05-09T12:00:00+0000type: photolink:https://www.facebook.com/the.../photos/a.715704...1.1073741846.29089766../1421...747/?type=3id: 290897664260544 1421818854501747likes count: 46362comments count: 237shares count: 8194
Complete output in a text file: ClickHere
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 31 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
getPost()
Definition
getPost() retrieves information about a public Facebook post,including list of comments and likes.
Syntax: getPost(post, token, n, comments = TRUE,
likes = TRUE, n.likes = n, n.comments = n)
From above token means Access token, n indicates number ofmaximum posts retrieve and post indicates postID.
This method gives output as "post", "likes",
"comments"
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 32 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract Likes and Comment details of a Post
Source coderequire("Rfacebook")
load("fb oauth")
postid="1909681024XX 54558XX"
Postdetails < − getPost(postid,
fb oauth, n = 10, comments =
TRUE, likes = TRUE,n.likes = n,
n.comments = n)
Postdetails # printing
Outputid: 1909681024XX 54558XXlikes count: 112214comments count: 1235share count: 124
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 33 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
getFriends()
Definition
getFriends() retrieves information about the user’s friends.
Syntax: getFriends(token, simplify = FALSE)
From above token means Access token, simplify If TRUE,function will return only name and id for each friend. IfFALSE, it will return additional information from theirprofiles: gender, birthday, location, hometown, relationshipstatus and profile picture.This function requires the use of a OAuth token withextended permissions and only friends who are using theapplication (Facebook App) that you used to generate thetoken to query the API will be returned.
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 34 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract Friends Information
Source coderequire("Rfacebook")
load("fb oauth")
my friends < −getFriends(token=fb oauth,
simplify = TRUE)
my friends # printing output
OutputCurrently no user accessing App.so No output
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 35 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Facebook Profile Picture Extraction
If we want to extract facebook profile picture we need httr packagealong with Rfacebook it is also installed. In this package we haveto use two function: GET() and content() to extract the image.
Syntax of GET():GET(url = NULL, config = list(), ..., handle = NULL)
Syntax of content():content(x, as = NULL, type = NULL, encoding = NULL,
...)
We get URL of profile picture from Graph API toolhttps://graph.facebook.com/me/picture?fields=url,redirect=FALSE
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 36 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Example to Extract Profile Picture from Facebook
Source codequery < −’https://graph.facebook.com/me
/picture? fields=
url,redirect=FALSE’
z = GET(query, config=fb oauth)
z1 = content(z)
z1<-as.Image(z1) # Convert raw
data to image
image(rotate(z1,-90))
Output
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 37 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Extract All Images of Authenticated User at a time
library(Rfacebook)
library(RImageJROI)
library(EBImage)
query< −"https://graph.facebook.com/v2.6/me?fields=photos.limit(100)%7Bpicture%7D&access token=EAACEdEose0cBANsOCg0ufGUQaESdpwEyYs4vG"
z< −callAPI(query, fb oauth)
library("tm")
MC tokenizer(z[1][1]$photos[1][[1]])
scan tokenizer(z[1][1]$photos[1][[1]])
strsplit space tokenizer < − function(x)
unlist(strsplit(as.String(x), " "))
z4< −strsplit space tokenizer(z[1][1]$photos[1][[1]])
z5< −matrix(z4, ncol = 4, byrow = TRUE)
detach("package:tm", unload=TRUE)
for(i in 1:100){ query< −z5[i,2]z< −GET(query, config=fb oauth)
z1< − as.Image(content(z))
z1< −image(rotate(z1,-90))}
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 38 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Output of all images
Display all images one on another.
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 39 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
Other Functions in Rfacebook Package
Following packages are not working in current version of GraphAPI Tool due to security& privacy issue.
getCheckins() retrieves information about a friend’scheckins
getInsights() Extract Insights metric from a Facebookpage (admin role required)
getFQL() connects to Facebook’s Graph API and executes aFQL query.
searchFacebook() retrieves public status updates thatmention a given keyword
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 40 / 41
Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others
thank You
Yogi Vemana University, Proddatur, Kadapa
Facebook Data Analytics using R
July 29, 2016 Slide: 41 / 41