The Rfacebook package, developed and maintained by Pablo Barberá, lets you easily establish and take advantage of Facebook's API thanks to a series of functions.
As we did for the twitteR package, we are going to establish a connection with the API and retrieve posts pertaining to a given keyword.
This recipe will mainly be based on functions from the Rfacebok package. Therefore, we need to install and load this package in our environment:
install.packages("Rfacebook") library(Rfacebook)
After skipping the quick start (the button on the upper-right corner), you can see the settings of your app and take note of app_id
and app_secret
, which you will need in order to establish a connection with the app.
fbOAuth()
function as follows:fb_connection <- fbOauth(app_id = "your_app_id", app_secret = "your_app_secret") fb_connection
Running the last line of code will result in a console prompt, as shown in the following lines of code:
copy and paste into site URL on Facebook App Settings: http://localhost:1410/ When done press any key to continue
Following this prompt, you will have to copy the URL and go to your Facebook app settings.
Once there, you will have to select the Settings tab and create a new platform through the + Add Platform control. In the form, which will prompt you after clicking this control, you should find a field named Site Url. In this field, you will have to paste the copied URL.
Close the process by clicking on the Save Changes button.
At this point, a browser window will open up and ask you to allow access permission from the app to your profile. After allowing this permission, the R console will print out the following code snippet:
Authentication complete Authentication successful.
data.frame
for further analysis.Among other useful functions, Rfacebook provides the searchPages()
function, which as you would expect, allows you to search the social network for pages mentioning a given string.
Different from the searchTwitter
function, this function will not let you specify a lot of arguments:
string
: This is the query stringtoken
: This is the valid OAuth token created with the fbOAuth()
functionn
: This is the maximum number of posts to be retrievedpages ← searchPages('data science with R',fb_connection) hist(pages$likes)
To search for data science with R, you will have to run the following line of code:
This will result in data.frame
storing all the pages retrieved along with the data concerning them.
As seen for the twitteR package, we can take a quick look at the like distribution, leveraging the base R hist()
function:
This will result in a plot similar to the following:
Refer to the data visualization section for further recipes on data visualization.