You are on page 1of 41

Outline Introduction Permissions User Details Status Likes Groups Pages NewsFeed Posts Friends Picture Others

Facebook Data Analytics using R

Dr. C. Naga Raju


Associate Professor & Head

Praveen Kumar D
Research Scholar
Department of Computer Science & Engineering
YSREC of Yogi Vemana University, Proddatur
Kadapa Dt., A. P, India

July 29, 2016


Yogi 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 users 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 are


require
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 using


install.packages(Package Name) function. i.e
install.packages(Rfacebook)
install.packages(RCrul)
Load Rfacebook & RCrul packages in R using following code
require(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 the


facebook account.
For accessing facebook data we used one tool it is Graph API
Explore 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, the
app will be able to obtain an access token which provides
temporary, 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 by
Click on Get Token button, When we click on Get token following
screen 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 click
on 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 few
options like change access permission of logged user and also
shown 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 further
accessing 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 valid
up to 120 minutes.
If we want a permanent access key we need to create an
Facebook app and from there we get the access key.
(https://developers.facebook.com/apps/) for more details
Here 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 button


we 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 more
Facebook users and private information of the main User.

After version 2.0 of the Facebook API, only id, name, and
picture are available through the API. All the remaining fields
will 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 details
it 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 code OUTPUT
require("Rfacebook") Id: 57142XXXXX
load("fb oauth") name: PraveenKumar Donta
username: NA
me< getUsers("me",token= first name: PraveenKumar
fb oauth ) middle name: NA
me # printing output last name: Donta
gender : male
locale : en US
category : NA
likes : NA
picture : 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 code Output
require("Rfacebook") Id: 57142XXXXX
name: PraveenKumar Donta
load("fb oauth") username: NA
me< getUsers("me",token= first name: PraveenKumar
middle name: NA
fb oauth,private info=T) last name: Donta
me # printing output gender : male
locale : en US
category : NA
likes : NA
picture : NA
birthday : 04/21/1990
Location : Cumbum, A. P, India
hometown : Giddalur
relationship 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 the
Facebook profile of the authenticated user.

Syntax: updateStatus(text, token, link = NULL)


From above text is a string of message we want to post
status. 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 code Output


require("Rfacebook")
load("fb oauth")
text< "Hi.. This post is
posted from R"

updateStatus(text,fb oauth, link


= NULL)

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 with


R
Source code Output
require("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)

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 friends likes. To retrieve
the number of likes for a page

Syntax: getLikes(user, n , fb oauth )


From above user is any valid user id, fb oauth means Access
token and n indicates max number of like you want to
retrieve.
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 code Output


require("Rfacebook")
load("fb oauth")
likes< getLikes(me, n = 500,
token= fb oauth )

table(likes)

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 public
posts on Facebook groups.

Syntax: searchGroup(name, token)


From above name is any valid String, token means Access
token
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 code Output


require("Rfacebook")
Name Privacy ID
load("fb oauth")
Indian Job Seekers CLOSED 664407993570414
name< "india" INDIAN PAINT- OPEN 162815470570453
INGS
groups< searchGroup(name, Jobs In India OPEN 655519397913849
fb oauth ) IEEE Computer CLOSED 338878412832563
Society - India
table(groups) Council
Indian superstar CLOSED 238349126246139
prince mahesh
babu fans
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, token
means 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 code Output
require("Rfacebook") from id: 1700931806812215
from name: Swarna Cherukuri
load("fb oauth") message:
group id=655519397913849 http://tollywoodmonkeys.com/jet-airways-walkin-
freshers-for-cabin-crew-from-dec-1-to-dec-12/
groupdetails< getGroup(group id created time: 2015-12-06T05:22:49+0000
, token=fb oauth, n = 100, since type: link
= NULL, until = NULL) link:
http://tollywoodmonkeys.com/jet-airways-walkin-
freshers-for-cabin-crew-from-dec-1-to-dec-12/
groupdetails
id: 655519397913849 743777472421374
likes count: 20
comments 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 Access
token 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 a


string

Source code Output


Id: 14483644XXX
require("Rfacebook") about: Indian Space Research Organization
category: Govt. Org.
load("fb oauth") description: Driven by inquisitiveness, Mankinds....
general info:NA likes: 1117479
pages < searchPages( link: https://www.facebook.com/ISRO/
city: Bangalore
string="university", token= state: NA
fb oauth, n=500 ) country: India
latitude: 13.03825
longitude:77.56492
pages name: ISRO - Indian Space Research Organisation
talking about count: 6378
username: 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. Note
that 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 Access
token and n indicates retrieve maximum number of post in a
page .
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 Facebook


page

Source code Output


from id: 1448364408720250
require("Rfacebook") from name: ISRO - Indian Space Research Organisation
message: A Kannada poem about the NAVIC
load("fb oauth") constellati....
created time: 2016-05-04T05:44:52+0000
page< 1448364408720250 type: photo
link:
pdetails < getPage(page, https://www.facebook.com/ISRO/photos/a.1448404935382864.1
id: 1448364408720250 1727183020838386
fb oauth, n = 10, since = NULL,
until = NULL, feed = FALSE) likes count: 672 comments count: 69 shares count: 124

pdetails # printing

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 authenticated
users News Feed

Syntax: getNewsfeed(token, n)
From above token means Access token (Must be the extended
access token) and n indicates number of maximum posts
retrieve.
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 Facebook


page

Source code Output


from id: 290897664260544
require("Rfacebook") from name: The BACK Benchers
to id: NA
load("fb oauth") to name: NA
message: THAT DIFFERENCE...
news < created time: 2016-05-09T12:00:00+0000
type: photo
getNewsfeed(token=fb oauth) link:
https://www.facebook.com/the.../photos/a.715704...1.10737418
id: 290897664260544 1421818854501747
news # printing likes count: 46362
comments count: 237
shares 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 of
maximum 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 code Output


require("Rfacebook") id: 1909681024XX 54558XX
load("fb oauth") likes count: 112214
postid="1909681024XX 54558XX" comments count: 1235
Postdetails < getPost(postid, share count: 124
fb oauth, n = 10, comments =
TRUE, likes = TRUE,n.likes = n,
n.comments = n)

Postdetails # printing

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 users 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. If
FALSE, it will return additional information from their
profiles: gender, birthday, location, hometown, relationship
status and profile picture.
This function requires the use of a OAuth token with
extended permissions and only friends who are using the
application (Facebook App) that you used to generate the
token 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 code Output


require("Rfacebook") Currently no user accessing App.
load("fb oauth") so No output
my friends <
getFriends(token=fb oauth,
simplify = TRUE)

my friends # printing 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 package


along with Rfacebook it is also installed. In this package we have
to 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 tool
https://graph.facebook.com/me/picture?fields=url,redire

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 code Output


query <
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))

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 Graph


API Tool due to security& privacy issue.
getCheckins() retrieves information about a friends
checkins
getInsights() Extract Insights metric from a Facebook
page (admin role required)
getFQL() connects to Facebooks Graph API and executes a
FQL query.
searchFacebook() retrieves public status updates that
mention 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

You might also like