Get Started with Rfacebook Package in R
Rfacebook package of R has many capabilities, here we have covered basic and very important part to start with Rfacebook. Connect with Facebook Graph APIs, collect posts and collect statistics regarding any post.
Here is very quick walkthrough how to start with Rfacebook package and connect facebook graph API:
- R , RStudio
- Install Rfacebook package
- Facebook account to authenticate. Your personal facebook account will be sufficient.
- Facebook User App, within developer facebook page and create new app
Create App: Go to https://developers.facebook.com/apps/
- Click on “Add New App”
- Choose category, for this example I’ve chosen “Reference” category. Need not to bother much about this, for “API” interaction anyone can work.
- Once the app will create, Click on App and go to dashboard. Here you’ll find the app id, secret key and password which you can use for API interaction.
- Go to App Setting and click on Add platform
- Once you’ll click on Add Platform, below window will pop. Here you’ve to click on Website symbol and provide Site URL: http://localhost:1410/
This url will be used to interact Rfacebook library with Facebook API while authentication process.
All prerequisite configuration has been done. Now we are ready to write R Script.
- Start running following command to create authorization variable
1234library(Rfacebook)app_id = "18XXXXXXXXXXXXX62"app_secret = "eXXXXXXXXXXXXXXXXXXXXXXX9"myauth <- fbOAuth(app_id = app_id, app_secret = app_secret)
- Go to your facebook app page and see the dashboard and you’ll get App ID and App Secret.
Here the app_id is your Facebook App ID and app_secret is your facebook App secret. Please see below :
- We have created the persistent app_secret which doesn’t have any expiration date. Although there is another method of creating an access_token which expires after 2 hours.Now start RStudio and run following code by providing your app_id and app_secret. The following shown app_id and app_secret are hidden. It is just for sample don’t try to copy the same use, it won’t work. 🙂
1myauth <- fbOAuth(app_id = app_id, app_secret = app_secret)
Hit enter, it will open your default browser and make sure you are connected to internet. API extraction will only work in online mode.
- Now not to run these process every time, we must save our authenticated variable locally. i.e., “myauth”. You can save it into your local directory.
1save(myauth, file="<Your Directory Path>/FbAuth")
- And then reload and from next time only start after load command:
1load("<Your Directory Path>/FbAuth")
- You can now start working with Facebook API and extract the information of Public Pages by providing the page.
- To get the page_id or entity_id , we must look into the html source code of that public page. And search for entity_id or page_id. I’ve taken https://www.facebook.com/Googlecom-230785066962247/ for sample URLIt has opened the page which I’m looking to analyze into chrome browser. For chrome press ctrl+U to see the source code. And then search (Ctrl+F) page_id or entity_id whichever is present copy the page id. Now start using it for analysis.
- From step #: 9, we got the page id. This is used to get the Page information.
12page_id = "230785066962247"page_info <- getPage(page_id, token = myauth, n=10, feed=TRUE, reactions=TRUE)
I’ve done it for n=10, which is 10 number of public posts.
Also you can provide date since and until
- See the below dataset which is received from getPage function. It is very easy to use and start with:
- There are other useful function available in Rfacebook package such as getPost and comments received.
123456post_id = "230785066962247_228027730654038"post_info <- getPost(post_id, token = myauth)post_info$comments$message## Output Received##  "GOOD BYE GOOGLE , I WAS A FAN BUT NO MORE https://www.facebook.com/SourceFedNews/videos/1199514293432055/\n'Did Google manipulate search for Hillary? This is perhaps the biggest story we've ever reported.'"
Please see other useful link for more information: