This document provides instructions for setting up APIs in R for Facebook, Twitter, and YouTube data. It explains how to download and install R and RStudio, set up the Facebook and Twitter APIs by obtaining app IDs and secrets, and provides code samples to download the first batch of data from each platform, such as 100 Facebook posts or a specified number of tweets. It also discusses saving the social media data to CSV files.
1. Set up R and API authorization
Download and install R:
https://cran.r-project.org/mirrors.html
Download and install RStudio:
https://www.rstudio.com/
1
2. Source: view/edit your R script
Console: show progress in data
processing and output
Environment: a list of things from
running R scripts
2
3. Set up R and API authorization
Use [COMMAND]+[ENTER] key on Mac, and
[CONTROL]+[ENTER] key on PC to run a R script one line
at a time.
To run multiple lines, just select them
3
10. Download your first bath of Facebook data
You are all set to mine Facebook data!
By executing the above code, we ask R to download 100 posts sent between
07/05/2016 and 09/11/2016 by the Facebook page UMassAmherst. We set
reactions to TRUE so that R will get the total count of Facebook reactions: love,
haha, wow, sad, angry. The output is named Umass_post.
10
11. The code creates a data frame named UMass_post. A
data frame is a spreadsheet-list R object.
Download your first bath of Facebook data
11
15. Download your first bath of Facebook data
The CSV file is saved in the current working directory. Use getwd() to find the
file path pointing to the directory
15
16. Set up Twitter API in R
Go to https://apps.twitter.com/
16
21. Download your first bath of tweets
21
• What is the keyword used in the search?
• How many tweets are we asking R to retrieve from Twitter API?
• If we want to retrieve only English tweets, how should we change the code?
• If we want to retrieve tweets sent between 9/1/2016 and 9/11/2016, how would you tweak
the code?
22. Download your first bath of tweets
22
• Use ncol() to find out how many columns does hashtagtweets.df have.
• Use nrow() to find out how many rows does hashtagtweets.df have.
• Use colnames() to find a list of column names in hashtagtweets.df.