knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

socialsampler

Lifecycle: experimental CRAN status Codecov test coverage R build status

socialsampler extends rtweet to gracefully handle multiple tokens, to automatically use bearer tokens for increased sampling capacity, and to automatically retry failed API requests.

Installation

You can install the development version of socialsampler from GitHub with:

# install.packages("devtools")
devtools::install_github("alexpghayes/socialsampler")

Sampling Twitter data with socialsampler

socialsampler provides (essentially) drop-in replacements for several rtweet functions. These drop-in functions follow the same naming conventions as rtweet functions, but with a safe_* prefix. For example:

library(socialsampler)

safe_get_friends("alexpghayes")

If you run into Twitter API rate limits, socialsampler will just wait for the rate limit to reset.

Registering and managing your tokens

The major functionality of socialsampler is its ability to use multiple tokens for data collection, automatically using tokens with remaining API calls. Additionally, socialsampler uses both user tokens and bearer tokens. Again, this is automatic, and should at least double sampling capacity relative to rtweet for most API requests.

Registering a token is straightforward:

register_token(
  consumer_key = "FILL_THIS_IN",
  consumer_secret = "FILL_THIS_IN",
  access_token = "FILL_THIS_IN",
  access_secret = "FILL_THIS_IN"
)

You only ever need to register a token once, and socialsampler won't accidentally add any duplicate tokens to the token database. To see how many tokens you have, you can use:

inspect_token_db()

You can import rtweet tokens into the database via import_tokens_from_rtweet(), and clear all tokens from the token database with unregister_all_tokens().

If no tokens have been registered with socialsampler, API calls will look for a token registered with rtweet and use that token instead, again automatically respecting rate limits and leveraging the corresponding bearer token. If you have not registered tokens either socialsampler or rtweet and you try to make an API request, you'll get an error.



alexpghayes/socialsampler documentation built on Jan. 3, 2022, 6:54 p.m.