Package Rfacebook
August 7, 2015
Title Access to Facebook API via R
Description Provides an interface to the Facebook API.
Version 0.6
Date 2015-08-04
Author Pablo Barbera <[email protected]>, Michael Piccirilli
<[email protected]>
Maintainer Pablo Barbera <[email protected]>
Depends R (>= 2.12.0), httr, rjson, httpuv
License GPL-2
NeedsCompilation no
Repository CRAN
Date/Publication 2015-08-07 09:01:09
R topics documented:
Rfacebook-package
fbOAuth . . . . . .
getCheckins . . . .
getFQL . . . . . .
getFriends . . . . .
getGroup . . . . .
getInsights . . . . .
getLikes . . . . . .
getNetwork . . . .
getNewsfeed . . . .
getPage . . . . . .
getPost . . . . . . .
getUsers . . . . . .
searchFacebook . .
searchGroup . . . .
searchPages . . . .
updateStatus . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
1
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2
2
4
5
5
6
7
9
10
11
11
12
14
15
16
17
18
fbOAuth
Index
Rfacebook-package
19
Access to Facebook API via R
Description
This package provides a series of functions that allow R users to access Facebooks API to get
information about users and posts, and collect public status updates that mention specific keywords.
Author(s)
Pablo Barbera <[email protected]>
See Also
fbOAuth, getUsers, getPost, searchFacebook, updateStatus, getFriends, getNetwork, getPage
fbOAuth
Create OAuth token to Facebook R session
Description
fbOAuth creates a long-lived OAuth access token that enables R to make authenticated calls to
the Facebook API. The token can be saved as a file in disk to be re-used in future sessions. This
function relies on the httr package to create the OAuth token, and is a simplified version of one of
its examples.
This function will automatically detect the API version for the token you create.
Usage
fbOAuth(app_id, app_secret, extended_permissions = TRUE)
Arguments
app_id
app_secret
numeric, App ID of application to be used to create OAUth token. Available at
https://developers.facebook.com/apps
string, App Secret of application to be used to create OAUth token. Available at
https://developers.facebook.com/apps, in Basic Settings panel.
extended_permissions
If TRUE, the token will give access to some of the authenticated users private
information (birthday, hometown, location, relationships) and that of his/her
friends, and permissions to post status updates as well as to access checkins,
likes, and the users newsfeed. If FALSE, token will give access only to public
information. Note that updateStatus will only work for tokens with extended
permissions.
fbOAuth
Details
There are two different ways of making authenticated requests. One is to obtain a temporary access token from https://developers.facebook.com/tools/explorer/, which can be used as
argument in any of the functions in Rfacebook. An example is shown below.
However, this token has a 2-hour lifetime by default and after it expires, it needs to be renewed. The
second alternative is to create an OAuth token. The process to create it is a bit more tedious. It is
divided in three steps.
First, go to https://developers.facebook.com/apps, register as a developer and create a new
app. You will also need a verified Facebook account. After that, click in "Show" under "App Secret"
to find your App ID and App Secret.
Second, run the fbOAuth function with your "App ID" and "App Secret" as arguments. It will
return a URL, which you will need to paste into the "Website with Facebook login" field in your
App Settings on Facebook. Once youve done so, press Enter.
Third, after pressing Enter, R will try to open a browser window to sign the token. If everything
works well, you will get a message that says you can return to R. If not, try again in a few minutes
to make sure your app had its settings updated properly.
Author(s)
Pablo Barbera <[email protected]>
See Also
getUsers, getPost, searchFacebook
Examples
## Not run:
## an example of an authenticated request after creating the OAuth token
## where app_id and app_secret are fictitious, and token is saved for
## future sessions
fb_oauth <- fbOAuth(app_id="123456789", app_secret="1A2B3C4D")
save(fb_oauth, file="fb_oauth")
load("fb_oauth")
me <- getUsers("me", token=fb_oauth)
me$username
## an example of a request using a temporary access token
token <- "XXXXXXAAAAAAA1111"
me <- getUsers("me", token=token)
## End(Not run)
getCheckins
getCheckins
Extract list of checkins of a Facebook friend
Description
getCheckins retrieves information about a friends checkins
Usage
getCheckins(user, n = 10, token, tags = FALSE)
Arguments
user
A user ID or screen name.
Maximum number of checkins to return for each user.
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
tags
If TRUE, output of function will be a list of two elements: a data frame with users
checkins and a list of data frames, where each element contains information
about users tagged in each checkin.
Details
This function requires the use of an OAuth token with the following permissions: user_status,
user_checkins, friends_status, friends_checkins
Check-in search was deprecated with version 2.0 of the Facebook Graph API.
Author(s)
Pablo Barbera <[email protected]>
See Also
getFriends
Examples
## Not run:
token <- 'XXXXX'
my_checkins <- getCheckins(user="me", token=token)
## End(Not run)
getFQL
getFQL
Executes a FQL query to the Facebook Graph API
Description
getFQL connects to Facebooks Graph API and executes a FQL query. See https://developers.
facebook.com/docs/technical-guides/fql/ for an overview of the Facebook Query Language.
Usage
getFQL(query, token)
Arguments
query
Text of query
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Author(s)
Pablo Barbera <[email protected]>
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Getting list of friends of authenticated user
load("fb_oauth")
d <- getFQL("SELECT uid2 FROM friend WHERE uid1=me()", token=fb_oauth)
## End(Not run)
getFriends
Extract list of friends with their information
Description
getFriends retrieves information about the users friends.
Usage
getFriends(token, simplify = FALSE)
getGroup
Arguments
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
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.
Details
This function requires the use of a OAuth token with extended permissions. After the introduction
of version 2.0 of the Graph API, only friends who are using the application that you used to generate
the token to query the API will be returned.
Author(s)
Pablo Barbera <[email protected]>
See Also
getUsers, fbOAuth
Examples
## Not run:
## Copy and paste token created at FB Graph API Explorer
token <- "XXXXXX"
my_friends <- getFriends(token=token, simplify=TRUE)
## Since users are ordered by ID, this will return 10 oldest user accounts
head(my_friends, n=10)
## End(Not run)
getGroup
Extract list of posts from a public Facebook group
Description
getGroup retrieves information from a public Facebook group.
Usage
getGroup(group_id, token, n = 100, since = NULL, until = NULL)
getInsights
Arguments
group_id
Facebook ID for the group. Note that this is different from the name on the
URL. You can use searchGroup to find the ID.
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Number of posts of page to return. Note that number can be sometimes higher
or lower, depending on status of API.
since
A UNIX timestamp or strtotime data value that points to the start of the time
range to be searched. For more information on the accepted values, see: http:
//php.net/manual/en/function.strtotime.php
until
A UNIX timestamp or strtotime data value that points to the end of the time
range to be searched. For more information on the accepted values, see: http:
//php.net/manual/en/function.strtotime.php
Author(s)
Pablo Barbera <[email protected]>
See Also
getUsers, getPost, fbOAuth
Examples
## Not run:
## Find Facebook ID for R-Users Facebook group
load("fb_oauth")
ids <- searchGroup(name="rusers", token=fb_oauth)
ids[1,] # id = 18533493739
## Downloading posts from R-Users Facebook group
group <- getGroup(group_id=18533493739, token=fb_oauth)
## Downloading posts from R-Users Facebook group in January 2013
group <- getGroup(group_id=18533493739, token=fb_oauth,
since='2013/01/01', until='2013/01/31')
## End(Not run)
getInsights
Extract Insights metric from a Facebook page (admin role required)
Description
getInsights retrieves information from an owned Facebook page. Note that you must specify
wich metric from insights you need and the period.
getInsights
Usage
getInsights(object_id, token, metric, period = "day", parms = NA, n = 5)
Arguments
object_id
An object (page, post, domain) ID.
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
metric
The metric which you want to get values for.
period
Time intervals to return
parms
Optional argument that can be used to append additional parameters. For example, &since=DATE&until=DATE.
Number of time intervals of metric values to return. Note that all metrics returned will be multiple of 3, except for lifetime period. Default n is 5
Details
The current list of supported metrics and periods is: page_fan_adds, page_fan_removes, page_views_login,
page_views_login, page_views_logout, page_views, page_story_adds, page_impressions, page_posts_impressions,
page_consumptions, post_consumptions_by_type, page_consumptions, and page_fans_country.
For more information, see: https://developers.facebook.com/docs/graph-api/reference/
v2.1/insights
Author(s)
Danilo Silva <[email protected]> Eduardo Carvalho <[email protected]> Andrew Geisler https://github.com/andrewgeisler
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Getting page impressions for Facebook's Facebook Page
## (only owner or admin of page)
load("fb_oauth")
insights <- getInsights(object_id="20531316728", token=fb_oauth, metric='page_impressions')
## Getting post impressions for a random Facebook's page post
## (only owner or admin of page)
insights <- getInsights(object_id='221568044327801_754789777921289',
token=fb_oauth, metric='post_impressions', period='days_28')
## Getting page fans for date range
## (only owner or admin of page)
insights <- getInsights(object_id='221568044327801',
token=fb_oauth, metric='page_fans', period='lifetime',
parms='&since=2015-01-01&until=2015-01-31')
## Count of fans by country
insights <- getInsights(object_id='221568044327801_754789777921289',
token=fb_oauth, metric='page_fans_country', period='lifetime')
getLikes
## End(Not run)
getLikes
Extract list of likes of a Facebook friend
Description
getLikes retrieves information about a friends likes. To retrieve the number of likes for a page,
use getUsers with the page IDs.
Usage
getLikes(user, n = 500, token)
Arguments
user
A user ID or screen name.
Maximum number of likes to return for each user.
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Details
This function requires the use of an OAuth token with the following permissions: user_likes,
friends_likes
Author(s)
Pablo Barbera <[email protected]>
See Also
getFriends, fbOAuth
Examples
## Not run:
token <- 'XXXXX'
my_likes <- getLikes(user="me", token=token)
## End(Not run)
10
getNetwork
getNetwork
Extract network of friends of authenticated user
Description
getNetwork retrieves the list of mutual friendships and returns the adjacency matrix or edge list for
the network representing the neighborhood of the authenticated user.
Usage
getNetwork(token, format = "edgelist", verbose = TRUE)
Arguments
token
format
verbose
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Either "edgelist" for a list of edges in the network or "adj.matrix" for an adjacenty matrix of dimensions (n x n), with n being the number of friends, and 0 or
1 indicating whether friend i is also friends with friend j.
logical, default is TRUE, which will print additional information on the console.
Details
This function requires the use of an OAuth token with extended permissions.
After the introduction of version 2.0 of the Graph API, only friends who are using the application
will be returned.
Author(s)
Pablo Barbera <[email protected]>
See Also
getFriends, fbOAuth
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Copy and paste token created at FB Graph API Explorer
token <- "XXXXXX"
mat <- getNetwork(token=token, format="adj.matrix")
library(igraph)
network <- graph.adjacency(mat, mode="undirected")
pdf("network_plot.pdf")
plot(network)
dev.off()
## End(Not run)
getNewsfeed
getNewsfeed
11
Download recent posts from the authenticated users newsfeed
Description
getNewsfeed retrieves status updates from the authenticated users News Feed
Usage
getNewsfeed(token, n = 200)
Arguments
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Maximum number of posts to return.
Author(s)
Pablo Barbera <[email protected]>
See Also
fbOAuth, getPost
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Capture 100 most recent posts on my newsfeed
load("fb_oauth")
my_newsfeed <- getNewsfeed(token=fb_oauth, n=100)
## End(Not run)
getPage
Extract list of posts from a public Facebook page
Description
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.
Usage
getPage(page, token, n = 100, since = NULL, until = NULL, feed = FALSE)
12
getPost
Arguments
page
A page ID or page name.
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Number of posts of page to return. Note that number can be sometimes higher
or lower, depending on status of API.
since
A UNIX timestamp or strtotime data value that points to the start of the time
range to be searched. For more information on the accepted values, see: http:
//php.net/manual/en/function.strtotime.php
until
A UNIX timestamp or strtotime data value that points to the end of the time
range to be searched. For more information on the accepted values, see: http:
//php.net/manual/en/function.strtotime.php
feed
If TRUE, the function will also return posts on the page that were made by others
(not only the admin of the page).
Author(s)
Pablo Barbera <[email protected]>
See Also
getUsers, getPost, fbOAuth
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Getting information about Facebook's Facebook Page
load("fb_oauth")
fb_page <- getPage(page="facebook", token=fb_oauth)
## Getting posts on Humans of New York page, including posts by others users
## (not only owner of page)
page <- getPage(page="humansofnewyork", token=fb_oauth, feed=TRUE)
## Getting posts on Humans of New York page in January 2013
page <- getPage(page="humansofnewyork", token=fb_oauth, n=1000,
since='2013/01/01', until='2013/01/31')
## End(Not run)
getPost
Extract information about a public Facebook post
Description
getPost retrieves information about a public Facebook post, including list of comments and likes.
getPost
13
Usage
getPost(post, token, n = 500, comments = TRUE, likes = TRUE,
n.likes = n, n.comments = n)
Arguments
post
A post ID
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
numeric, maximum number of comments and likes to return.
comments
logical, default is TRUE, which will return data frame with comments to the post.
likes
logical, default is TRUE, which will return data frame with likes for the post.
n.likes
numeric, maximum number of likes to return. Default is n.
n.comments
numeric, maximum number of likes to return. Default is n.
Details
getPost returns a list with three components: post, likes, and comments. First, post contains
information about the post: author, creation date, id, counts of likes, comments, and shares, etc.
Second, likes is a data frame that contains names and Facebook IDs of all the users that liked the
post. Finally, comments is a data frame with information about the comments to the post (author,
message, creation time, id).
Author(s)
Pablo Barbera <[email protected]>
See Also
getUsers, getPage, fbOAuth
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Getting information about Facebook's Facebook Page
load("fb_oauth")
fb_page <- getPage(page="facebook", token=fb_oauth)
## Getting information and likes/comments about most recent post
post <- getPost(post=fb_page$id[1], n=2000, token=fb_oauth)
## End(Not run)
14
getUsers
getUsers
Extract information about one or more Facebook users
Description
getUsers retrieves public information about one or more Facebook users.
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.
Usage
getUsers(users, token, private_info = FALSE)
Arguments
users
A vector of user IDs.
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
private_info
If FALSE, will return only information that is publicly available for all users
(name, gender, locale, profile picture). If TRUE, will return additional information for users who are friends with the authenticated user: birthday, location,
hometown, and relationship status. Note that these fields will ONLY be returned
for friends and when the version of the token that is used to query the API is 1.0.
For other users, they will be NA, even if they are visible on Facebook via web.
Author(s)
Pablo Barbera <[email protected]>
See Also
getFriends, getPost, searchFacebook
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Getting information about the authenticated user
load("fb_oauth")
fb <- getUsers("me", token=fb_oauth)
fb$username
## End(Not run)
searchFacebook
searchFacebook
15
Search public posts that mention a string
Description
searchFacebook retrieves public status updates that mention a given keyword
Usage
searchFacebook(string, token, n = 200, since = NULL, until = NULL)
Arguments
string
string or string vector containing keywords to search. Note that the returned
results will contain any of the keywords. It is not possible to search for status
updates that include all of the keywords.
token
An OAuth token created with fbOAuth. Only tokens created for version 1.0 of
the Facebook Graph API will return results.
Maximum number of posts to return.
since
A UNIX timestamp or strtotime data value that points to the start of the time
range to be searched. For more information on the accepted values, see: http:
//php.net/manual/en/function.strtotime.php
until
A UNIX timestamp or strtotime data value that points to the end of the time
range to be searched. For more information on the accepted values, see: http:
//php.net/manual/en/function.strtotime.php
Details
Note: Public post search was deprecated with version 2.0 of the Facebook Graph API, and therefore
this function will no longer work. For more information about these changes, go to: https://
developers.facebook.com/docs/apps/changelog
The function will only work for OAuth tokens generated with version 1.0 of the API The search is
performed also on the text of the comments too, which explains why some of the returned messages
do not mention the string that is being searched.
Note that only messages up to around two weeks old or less can be returned.
Author(s)
Pablo Barbera <[email protected]>
See Also
fbOAuth
16
searchGroup
Examples
## Not run:
## Searching 100 public posts that mention "facebook"
posts <- searchFacebook( string="facebook", token=fb_oauth, n=100 )
## Searching 100 public posts that mention "facebook" from yesterday
posts <- searchFacebook( string="facebook", token=fb_oauth, n=100,
since = "yesterday 00:00", until = "yesterday 23:59" )
## End(Not run)
searchGroup
Find Facebook ID of a group
Description
Use searchGroup in combination with getGroup to scrape public posts on Facebook groups.
Usage
searchGroup(name, token)
Arguments
name
Name of Facebook group (in URL)
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Examples
## Not run:
## Find Facebook ID for R-Users Facebook group
load("fb_oauth")
ids <- searchGroup(name="rusers", token=fb_oauth)
ids[1,] # id = 18533493739
## Downloading posts from R-Users Facebook group
group <- getGroup(group_id=18533493739, token=fb_oauth)
## Downloading posts from R-Users Facebook group in January 2013
group <- getGroup(group_id=18533493739, token=fb_oauth,
since='2013/01/01', until='2013/01/31')
## End(Not run)
searchPages
searchPages
17
Search pages that mention a string
Description
searchPages retrieves public pages that mention a given keyword
Usage
searchPages(string, token, n = 200)
Arguments
string
string or string vector containing keywords to search. Note that the returned
results will contain any of the keywords.
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth.
Maximum number of pages to return.
Author(s)
Pablo Barbera <[email protected]>, Joel Gombin <[email protected]>
See Also
fbOAuth, searchFacebook
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
## Searching 100 public pages that mention "facebook"
load("fb_oauth")
pages <- searchPages( string="facebook", token=fb_oauth, n=100 )
## End(Not run)
18
updateStatus
updateStatus
Update Facebook status from R
Description
updateStatus sends a status update that will be displayed on the Facebook profile of the authenticated user.
Usage
updateStatus(text, token, link = NULL)
Arguments
text
string, text of the status update
token
Either a temporary access token created at https://developers.facebook.
com/tools/explorer or the OAuth token created with fbOAuth. It needs to
have extended permissions in order to successfully post to the Facebook profile.
link
string, URL of link to be added to status update
Author(s)
Pablo Barbera <[email protected]>, Zakharov Kyrylo (https://github.com/Amice13)
See Also
getUsers, getPost
Examples
## Not run:
## See examples for fbOAuth to know how token was created.
load("fb_oauth")
updateStatus("this is just a test", token=fb_oauth)
## End(Not run)
Index
fbOAuth, 2, 2, 6, 7, 913, 15, 17
getCheckins, 4
getFQL, 5
getFriends, 2, 4, 5, 9, 10, 14
getGroup, 6
getInsights, 7
getLikes, 9
getNetwork, 2, 10
getNewsfeed, 11
getPage, 2, 11, 13
getPost, 2, 3, 7, 11, 12, 12, 14, 18
getUsers, 2, 3, 6, 7, 12, 13, 14, 18
Rfacebook (Rfacebook-package), 2
Rfacebook-package, 2
searchFacebook, 2, 3, 14, 15, 17
searchGroup, 16
searchPages, 17
updateStatus, 2, 18
19