Use twitter from R. Get started by reading
vignette("rtweet")
.
To get the current released version from CRAN:
install.packages("rtweet")
You can install the development version of rtweet from GitHub with:
install.packages("rtweet", repos = 'https://ropensci.r-universe.dev')
All users must be authenticated to interact with Twitter’s APIs. The
easiest way to authenticate is to use your personal twitter account -
this will happen automatically (via a browser popup) the first time you
use an rtweet function. See auth_setup_default()
for
details. Using your personal account is fine for casual use, but if you
are trying to collect a lot of data it’s a good idea to authentication
with your own Twitter “app”. See
vignette("auth", package = "rtweet")
for details.
library(rtweet)
rtweet should be used in strict accordance with Twitter’s developer terms.
Search for up to 1000 tweets containing #rstats, the common hashtag used to refer to the R language, excluding retweets:
<- search_tweets("#rstats", n = 1000, include_rts = FALSE) rt
Twitter rate limits cap the number of search results returned to
18,000 every 15 minutes. To request more than that, set
retryonratelimit = TRUE
and rtweet will wait for rate limit
resets for you.
Search for 200 users with the #rstats in their profile:
<- search_users("#rstats", n = 200) useRs
Randomly sample (approximately 1%) from the live stream of all tweets:
<- stream_tweets("") random_stream
Stream all geo-located tweets from London for 60 seconds:
<- stream_tweets(location = lookup_coords("london"), timeout = 60) stream_london
Get all accounts followed by a user:
## get user IDs of accounts followed by R Foundation
<- get_friends("_R_Foundation")
R_Foundation_fds
## lookup data on those accounts
<- lookup_users(R_Foundation_fds$to_id) R_Foundation_fds_data
Get all accounts following a user:
## get user IDs of accounts following R Foundation
<- get_followers("_R_Foundation", n = 100)
R_Foundation_flw <- lookup_users(R_Foundation_flw$from_id) R_Foundation_flw_data
If you want all followers, you’ll need you’ll need to set
n = Inf
and retryonratelimit = TRUE
but be
warned that this might take a long time.
Get the most recent 200 tweets from R Foundation:
## get user IDs of accounts followed by R Foundation
<- get_timeline("_R_Foundation", n = 100) tmls
Get the 10 most recently favorited statuses by R Foundation:
<- get_favorites("_R_Foundation", n = 10) favs
Communicating with Twitter’s APIs relies on an internet connection, which can sometimes be inconsistent.
If you have questions, or needs an example or want to share a use case, you can post them on rOpenSci’s discuss. Were you can browse uses of rtweet too.
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.
Please note that this package is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.