In this blog we will learn how to analyze the comments of Public Facebook post using Facebook Graph API Explorer & R!
Facebook developer account required to get started with this Facebook Graph API .
If you don’t have Facebook developer account, you can upgrade your personal Facebook account to a Facebook Developer account from here this link.
After registering as Facebook Developer, go to “Tools & Support”->”Graph API Explorer”
To explore Graph API – Token & Permissions are required, so just click on the “Get Token”.
~ Courtesy Facebook Developer
As public profiles included by default in permissions, just click on “Get Access Token”.
Below is my access token, which will expire after some time. As shown in token info.
Graph Explorer
Now we have token, let’s explore now.
Extracting Comments from the Public Facebook Post.
First thing you required is the Post Id of the post. See below steps to get the post Id.
Suppose below is the post, we want to analyze. Click on the Post Date Time. See below highlighted box. ~ Post Courtesy Facebook
Copy below Id. This is the post Id.
Go to the Graph Explorer.
Type “Post_id/comments” in below box & click on Submit.
You can also give limit for number of comments to return, like this
“Post_id/comments?limit=”.
My Input:
Output: Below are post comments in the JSON format.
If you want more comments click on “next” for next page of comments.
Click on “Get Code” to get the cURL code. Copy this URL, we will use this URL in R.
Text Analysis in R
R Packages required:
install.packages(“RCurl “): It allows us to compose general HTTP requests and provides convenient functions to fetch data.
install.packages(“rjson”): It allows us to converts JSON object into R objects and vice-versa.
install.packages(“tm”): A Mining Package for text mining applications within R. It offers a number of transformations that ease the tedium of cleaning data.
R Commands:
url used in above image is copied from cURL code from Graph Explorer.
Output: So the first page give me 25 comments only. We will analyze here only 1st page i.e. 25 comments only.
Cleaning & Analyzing Data:
Creating corpus & removing extra spaces, special characters & other unwanted things.
Creating Term Document Matrix:
Here is the 760 extracted words with frequency.
Creating Wordcloud:
install.packages(“wordcloud”): For plotting a word cloud
In this Word Cloud we are taking only 100 words with minimum frequency of 2.
In this blog we will learn the basics of extracting Facebook data using R & Facebook API.
Rfacebook Package: Provides an interface to the Facebook API.
Rfacebook package in R provides functions that allow R to access Facebook’s API to get information about posts, comments, likes, group that mention specific keywords & much more.
Install “Rfacebook” package from CRAN : install.packages(“Rfacebook”)
Step 1: Registering an Application with Facebook.
If you already have an account with Facebook, go to FacebookDeveloper and register.
Click “Register Now” button. After you register as a Facebook developer, you can register a new application.
Register a new application
From FacebookDeveloper click on Apps at the top of the page to go to the application dashboard.
Click the button near the top. Once you are done with the verification process, your application is created. Note down the App Id & App Secret.