R client for accessing Twitter’s REST and stream APIs. Check out the rtweet package documentation website.
To get the current released version from CRAN:
To get the current development version from Github:
NEW: All you need is a Twitter account and rtweet and you’re up and running!
Search for up to 18,000 (non-retweeted) tweets containing the rstats hashtag.
Quickly visualize frequency of tweets over time using
## plot time series of tweets ts_plot(rt, "3 hours") + ggplot2::theme_minimal() + ggplot2::theme(plot.title = ggplot2::element_text(face = "bold")) + ggplot2::labs( x = NULL, y = NULL, title = "Frequency of #rstats Twitter statuses from past 9 days", subtitle = "Twitter status (tweet) counts aggregated using three-hour intervals", caption = "\nSource: Data collected from Twitter's REST API via rtweet" )
Twitter rate limits cap the number of search results returned to 18,000 every 15 minutes. To request more than that, simply set
retryonratelimit = TRUE and rtweet will wait for rate limit resets for you.
Search by geo-location—for example, find 10,000 tweets in the English language sent from the United States.
## search for 10,000 tweets sent from the US rt <- search_tweets( "lang:en", geocode = lookup_coords("usa"), n = 10000 ) ## create lat/lng variables using all available tweet and profile geo-location data rt <- lat_lng(rt) ## plot state boundaries par(mar = c(0, 0, 0, 0)) maps::map("state", lwd = .25) ## plot lat and lng points onto state map with(rt, points(lng, lat, pch = 20, cex = .75, col = rgb(0, .3, .7, .75)))
Randomly sample (approximately 1%) from the live stream of all tweets.
Stream all geo enabled tweets from London for 60 seconds.
Stream all tweets mentioning realDonaldTrump or Trump for a week.
Retrieve a list of all the accounts a user follows.
Retrieve a list of the accounts following a user.
Or if you really want ALL of their followers:
Get the most recent 3,200 tweets from cnn, BBCWorld, and foxnews.
## get user IDs of accounts followed by CNN tmls <- get_timelines(c("cnn", "BBCWorld", "foxnews"), n = 3200) ## plot the frequency of tweets for each user over time tmls %>% dplyr::filter(created_at > "2017-10-29") %>% 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 October/November 2017", caption = "\nSource: Data collected from Twitter's REST API via rtweet" )
Get the 3,000 most recently favorited statuses by JK Rowling.
Search for 1,000 users with the rstats hashtag in their profile bios.
Communicating with Twitter’s APIs relies on an internet connection, which can sometimes be inconsistent. With that said, if you encounter an obvious bug for which there is not already an active issue, please create a new issue with all code used (preferably a reproducible example) on Github.