Learn R Programming

⚠️There's a newer version (2.0.0) of this package.Take me there.

rtweet

Use twitter from R. Get started by reading vignette("rtweet").

Installation

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')

Usage

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)
#> 
#> Attaching package: 'rtweet'
#> The following object is masked from 'package:graphics':
#> 
#>     symbols

rtweet should be used in strict accordance with Twitter’s developer terms.

Search tweets or users

Search for up to 1000 tweets containing #rstats, the common hashtag used to refer to the R language, excluding retweets:

rt <- search_tweets("#rstats", n = 1000, include_rts = FALSE)

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:

useRs <- search_users("#rstats", n = 200)

Stream tweets

Randomly sample (approximately 1%) from the live stream of all tweets:

random_stream <- stream_tweets("")

Stream all geo-located tweets from London for 60 seconds:

stream_london <- stream_tweets(location = lookup_coords("london"), timeout = 60)

Get friends and followers

Get all accounts followed by a user:

## get user IDs of accounts followed by R Foundation
R_Foundation_fds <- get_friends("_R_Foundation")

## lookup data on those accounts
R_Foundation_fds_data <- lookup_users(R_Foundation_fds$to_id)

Get all accounts following a user:

## get user IDs of accounts following R Foundation
R_Foundation_flw <- get_followers("_R_Foundation", n = 100)
R_Foundation_flw_data <- lookup_users(R_Foundation_flw$from_id)

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 timelines

Get the most recent 100 tweets from R Foundation:

## get most recent 100 tweets from R Foundation
tmls <- get_timeline("_R_Foundation", n = 100)

Get favorites

Get the 10 most recently favorited statuses by R Foundation:

favs <- get_favorites("_R_Foundation", n = 10)

Contact

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.

Code of Conduct

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.

Copy Link

Version

Install

install.packages('rtweet')

Monthly Downloads

2,141

Version

1.1.0

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Llu<c3><ad>s Revilla Sancho

Last Published

January 9th, 2023

Functions in rtweet (1.1.0)

do_call_rbind

Binds list of data frames while preserving attribute (tweets or users) data.
direct_messages

Get direct messages sent to and received by the authenticating user from the past 30 days
get_followers

Get user IDs for accounts following target user.
clean_tweets

Clean text of tweets
get_favorites

Get tweets liked/favorited by one or more users
create_token

Create custom Twitter OAuth token
entity

Extract methods
emojis

Defunct: Emojis codes and descriptions data.
direct_messages_received

(DEPRECATED) Get the most recent direct messages sent to the authenticating user.
flatten

flatten/unflatten data frame
langs

Defunct: Language codes recognized by Twitter data.
get_token

Fetch Twitter OAuth token
get_retweets

Get the most recent retweets/retweeters
get_friends

Get user IDs of accounts followed by target user(s).
get_mentions

Get mentions for the authenticating user.
get_timeline

Get one or more user timelines
get_trends

Get Twitter trends data.
invalidate_bearer

Invalidate bearer token
ids

Extract the ids
lookup_collections

Collections API
lookup_coords

Get coordinates of specified location.
lat_lng

Adds single-point latitude and longitude variables to tweets data.
lists_memberships

Get Twitter list memberships (lists containing a given user)
lists_members

Get Twitter list members (users on a given list).
lookup_friendships

Lookup friendship information between two specified users.
lookup_tweets

Get tweets data for given statuses (status IDs).
post_destroy

Delete status of user's Twitter account
parse_stream

Parser of stream
plain_tweets

Clean up character vector (tweets) to more of a plain text.
next_cursor

Extract cursor (for cursor based pagination)
network_data

Network data
my_friendships

Lookup friendship information between users.
lists_subscriptions

Get list subscriptions of a given user but does not include the user's own lists.
lists_users

Get all lists a specified user subscribes to, including their own.
post_follow

Follows target Twitter user.
post_favorite

Favorites target status id.
rtweet-package

rtweet: Collect Twitter data from R
rtweet_user

Authentication options
post_friendship

Updates friendship notifications and retweet abilities.
round_time

A generic function for rounding date and time values
read_twitter_csv

Read comma separated value Twitter data.
post_list

Manage Twitter lists
max_id

Extract min/max id (for id based pagination)
lists_statuses

Get a timeline of tweets authored by members of a specified list.
lists_subscribers

Get subscribers of a specified list.
search_tweets

Get tweets data on statuses identified via search query.
lookup_users

Get Twitter users data for given users (user IDs or screen names).
stopwordslangs

Defunct: Twitter stop words in multiple languages data.
rate_limit

Rate limit helpers
search_fullarchive

Premium Twitter searches
rules

Extract the streaming rules
previous_cursor

Previous cursor
trends_available

Available Twitter trends along with associated WOEID.
search_users

Search for users
user_block

Blocking or unblocking twitter users
tweets_with_users

Parsing data into tweets/users data tibbles
tweet_threading

Collect statuses contained in a thread
tweet_shot

Capture an image of a tweet/thread
tweet_embed

Create a Tweet Embed
ts_plot

Plots tweets data as a time series-like data object.
ts_data

Converts tweets data into time series-like data object.
stream

Streaming
stream_tweets2

A more robust version of stream_tweets
users_data

Get tweets from users, or users from tweets
post_message

Posts direct message from user's Twitter account
post_tweet

Posts status update to user's Twitter account
stream_tweets

Collect a live stream of Twitter data
write_as_csv

Save Twitter data as a comma separated value file.
as_screenname

Mark a user id as a screen name
auth_save

Save an authentication mechanism for use in a future session
Expansions

Expansions
TWIT_paginate_max_id

Pagination
auth_setup_default

Set up default authentication
auth_sitrep

Twitter Tokens sitrep
auth_get

Get the current authentication mechanism
auth_as

Set default authentication for the current session
Fields

Fields
bearer_token

Bearer token