library(rtweet) library(httr) library(tidyverse) # Twitter configuration # Set all the Configuration details to authorize your application to access Twitter data. access_token_secret <- "XXXX" #replace with your access token secret consumer_key <- "XXXX" #replace with your key consumer_secret <- "XXXX" #replace with your secret access_token <- "XXXX" #replace with your access token appname <- "DS4100_KD" ## access token method: create token and save it as an environment variable # create token named "twitter_token" twitter_token <- rtweet::create_token(app = appname, consumer_key = consumer_key, consumer_secret = consumer_secret, access_token = access_token, access_secret = access_token_secret) # Press 1 in the console to allow the file to access the credentials rstats_tweets <- search_tweets(q = "#rstats", n = 500) # view the first 3 rows of the dataframe head(rstats_tweets, n = 3) pol_tweets <- search_tweets(q = "#kavanaugh", n = 50) head(pol_tweets, n = 4) users_data(pol_tweets) us_tweets <-search_tweets("lang:en", geocode=lookup_coords("usa"), n =2000) us_tweets<- lat_lng(us_tweets) par(mar = c(0,0,0,0)) maps::map('state', lwd=.25) with(us_tweets, points(lng, lat, pch = 20, cex = .75, col = rgb(0, .3, .7, .75))) ts_plot(pol_tweets) pol_noretweets <- search_tweets("#kavanaugh", n = 50, include_rts = FALSE) # view the first 3 rows of the dataframe head(pol_noretweets, n = 4) # collect sample of tweets defaut time = 30 seconds stream_t <- stream_tweets("") #requires a Google Maps API key boston <- stream_tweets(lookup_coords("boston, usa")) y1stream_tweets( "realdonaldtrump,trump", timeout = 30, file_name = "tweetsabouttrump.json", parse = FALSE ) ## read in the data as a tidy tbl data frame djt <- parse_stream("tweetsabouttrump.json") # get followers for a specific account ## get user IDs of friends of CNN jobi_fds <- get_friends("Jobi_Pottery") ## lookup data on those accounts jobi_fds_data <- lookup_users(cnn_fds$user_id) ## lookup followers of an account ## get user IDs of accounts following Jobi Pottert ## limit it to the first 75,000 jobi_flw <- get_followers("Jobi_Pottery", n = 75000) ## lookup data on those accounts jobi_flw_data <- lookup_users(jobi_flw$user_id) tmls <- get_timelines(c("cnn", "BBCWorld", "foxnews"), n = 3200) ## plot the frequency of tweets for each user over time tmls %>% dplyr::filter(created_at > "2018-10-03") %>% dplyr::group_by(screen_name) %>% ts_plot("days", trim = 1L) + ggplot2::geom_point() + ggplot2::theme_minimal() + ggplot2::theme( legend.title = ggplot2::element_blank(), legend.position = "bottom", plot.title = ggplot2::element_text(face = "bold")) + ggplot2::labs( x = NULL, y = NULL, title = "Frequency of Twitter statuses posted by news organization", subtitle = "Twitter status (tweet) counts aggregated by day from Oct 2018 to Feb 2019", caption = "\nSource: Data collected from Twitter's REST API via rtweet" ) # get recently favored statuses jkr <- get_favorites("jk_rowling", n = 300) # search for a hashtag in a profile biography ## search for users with #rstats in their profiles usrs <- search_users("#rstudio", n = 1000) usrs <- search_users("#r,data visualization", n = 1000) # trenda for a location sf <- get_trends("boston") ## lookup users by screen_name or user_id users <- c("KimKardashian", "justinbieber", "taylorswift13", "espn", "JoelEmbiid", "cstonehoops", "KUHoops", "upshotnyt", "fivethirtyeight", "hadleywickham", "cnn", "foxnews", "msnbc", "maddow", "seanhannity", "potus", "epa", "hillaryclinton", "realdonaldtrump", "natesilver538", "ezraklein", "annecoulter") famous_tweeters <- lookup_users(users) ## preview users data famous_tweeters # extract most recent tweets data from the famous tweeters tweets_data(famous_tweeters) ## lookup users by screen_name or user_id users <- c("KimKardashian", "justinbieber", "taylorswift13", "espn", "JoelEmbiid", "cstonehoops", "KUHoops", "upshotnyt", "fivethirtyeight", "hadleywickham", "cnn", "foxnews", "msnbc", "maddow", "seanhannity", "potus", "epa", "hillaryclinton", "realdonaldtrump", "natesilver538", "ezraklein", "annecoulter") famous_tweeters <- lookup_users(users) ## preview users data famous_tweeters # extract most recent tweet data from the famous tweeters hadley <- lookup_users("hadleywickham")