Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
RStudio for R Statistical Computing Cookbook

You're reading from   RStudio for R Statistical Computing Cookbook Over 50 practical and useful recipes to help you perform data analysis with R by unleashing every native RStudio feature

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher
ISBN-13 9781784391034
Length 246 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Andrea Cirillo Andrea Cirillo
Author Profile Icon Andrea Cirillo
Andrea Cirillo
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Acquiring Data for Your Project 2. Preparing for Analysis – Data Cleansing and Manipulation FREE CHAPTER 3. Basic Visualization Techniques 4. Advanced and Interactive Visualization 5. Power Programming with R 6. Domain-specific Applications 7. Developing Static Reports 8. Dynamic Reporting and Web Application Development Index

Getting data from Facebook with the Rfacebook package

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.

Getting ready

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)

How to do it...

  1. In order to leverage an API's functionalities, we first have to create an application in our Facebook profile. Navigating to the following URL will let you create an app (assuming you are already logged in to Facebook): https://developers.facebook.com.

    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.

  2. After installing and loading the Rfacebook package, you will easily be able to establish a connection by running the 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.
    
  3. To test our API connection, we are going to search Facebook for posts related to data science with R and save the results within 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 string
    • token: This is the valid OAuth token created with the fbOAuth() function
    • n: This is the maximum number of posts to be retrieved
      pages ← searchPages('data science with R',fb_connection)
      hist(pages$likes)

    Note

    The Unix timestamp

    The Unix timestamp is a time-tracking system originally developed for the Unix OS. Technically, the Unix timestamp x expresses the number of seconds elapsed since the Unix Epoch (January 1, 1970 UTC) and the timestamp.

    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:

    How to do it...

    Refer to the data visualization section for further recipes on data visualization.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image