November 29th, 2017
Today we will be covering:
Sys.sleep()to deal with this).
Twitter has two APIs for obtaining data from tweets and users:
library(SocialMediaLab); library(tidyverse); library(stringr) library(twitteR); library(ROAuth); library(grid); library(streamR) api_key <- "API KEY GOES HERE" api_secret <- "API SECRET GOES HERE" access_token <- "ACCESS TOKEN GOES HERE" access_token_secret <- "ACCESS TOKEN SECRET GOES HERE" reqURL <- "https://api.twitter.com/oauth/request_token" accessURL<- "https://api.twitter.com/oauth/access_token" authURL<- "https://api.twitter.com/oauth/authorize"
Social Media Lab provides a “one-stop shop” for accessing social media data and transforming it for network analysis techniques. It works with:
Data from all of these can be collected and analyzed using the same syntax! See this introduction to see exactly how much it can do.
We will only cover Twitter today, but Facebook and Youtube are similar. Instagram is more difficult to get API access to, but possible in theory.
Social Media Lab has a simple workflow for collecting data:
Authenticate()using your credentials
Collect()where you can specify search parameters.
Create()to generate network data
It will perform the search and process the data into a usable data frame. This beats
JSON files yourself!
tweets_Trump <- Authenticate("twitter", api_key, api_secret, access_token, access_token_secret) %>% Collect(searchTerm="Trump", language="en", verbose=TRUE) %>% as_tibble() tweets_Trump %>% arrange(retweetCount) %>% select(text)
# A tibble: 1,500 x 1 text <chr> 1 Come and see Trump News and see what the orange menace has been up to - htt 2 Having exhausted every other line of attack, New Republic writer goes after 3 "Please tell Mocha to log out of Trump's account.\n\nCovfefe and Maram = Sy 4 Trump believes the trip was a home run where ever he went. He's delusional 5 The Loneliness of Donald Trump. Great read - take time to savour this brill 6 "What is covfefe? Donald Trump baffles with late night Twitter post\n\nhttp 7 @Hirazistable If we get married Trump won't let me make the decision to pul 8 @Mikel_Jollett Exactly! Trump is a disaster for the country and the world. 9 I like thinking big. If youÃ¢â¬â¢re going to be thinking anything, you mi 10 "Trump: \"How much covfefing does a guy gotta do to get some service around # ... with 1,490 more rows
tweets_UW <- Authenticate("twitter", api_key, api_secret, access_token, access_token_secret) %>% Collect(geocode="47.6552083,-122.30833,2mi", searchTerm=" ", language="en", verbose=TRUE) tweets_UW %>% arrange(retweetCount) %>% select(text)