20140505 - Victor Gau - R to access the social graph from facebook

Preview:

DESCRIPTION

Some basic concepts of using R to access the social graph from the Facebook.

Citation preview

R TO ACCESS THE SOCIAL GRAPH FROM FACEBOOK

Victor GauLightning Talk @ Kaohsiung useR! Meetup

2014/05/05

Outline

What is the Social Graph? Graph API Explained What is JSON? “R” to Parse JSON Spider Revisited “R” to Access the Social Graph Using Facebook Developer Tools Topics for Sharing Next Time

What is the Social Graph?

Business Insider: So What The Heck Is The 'Social Graph' Facebook Keeps Talking About?

Graph API Explained

APIs for accessing social graphHTTP requests are sent to access the social

graph (the responses are in JSON format)most requests are sent to

https://graph.facebook.com requests for videos are sent to

https://graph-video.facebook.com

https://developers.facebook.com/docs/graph-api/quickstart/v2.0

Graph API Explainedhttps://www.facebook.com/HTC

What is JSON?

JavaScript Object Notation http://en.wikipedia.org/wiki/JSON

“R” to Parse JSON

Install “rjson” packageinstall.packages(“rjson”)

Load “rjson” packagelibrary(rjson)

Functions to use:fromJSON()toJSON()newJSONParser()

Sample Code - Reading JSONdata = '{

"firstName": "John",

"lastName": "Smith",

"isAlive": true,

"age": 25,

"height_cm": 167.64,

"address": {

"streetAddress": "21 2nd Street",

"city": "New York",

"state": "NY",

"postalCode": "10021-3100"

},

"phoneNumbers": [

{ "type": "home", "number": "212 555-1234" },

{ "type": "fax", "number": "646 555-4567" }

]

}'

person <- fromJSON(data)

person$firstName

person$lastName

Spider Revisited# Load Required Packages

library(RCurl)

library(XML)

# Get Page Source

html = getURL("http://www.ptt.cc/bbs/R_Language/index.html")

# Parse to get information using XPath

xml = htmlParse(html)

Xpath = "//div[@class='title']/a//text()"

titles = xml [Xpath]

“R” to Access the Social Graph

# Load Required Packages

library(RCurl)

library(rjson)

# Retrieve Data (in JSON format)

data = getURL("https://graph.facebook.com/htc", ssl.verifypeer = FALSE )

# Read JSON

htc <- fromJSON(data)

“R” to Access Social Graph Access Token would be needed for

certain content in social graph most of the time.

Access Token can be retrieved by using graph explorer from Facebook Developer Tools.

Sample URL for retrieving information with Access Token:http://graph.facebook.com/me/friends?

access_token=XDFAFDFXXXXXXXDAFDA

Using Facebook Developer Tools

https://developers.facebook.com/tools/

DEMO

Topics for Sharing Next time Authentication Social Plugins Open Graph Protocol FBML XFBML FQL …

Thank you!