Search code examples
rtwitterrtweet

'max_id' error in rtweet package for search_30day() and search_premium() functions


I am attempting to pull tweets from the last 3 years using R for a search term, and I have a Twitter sandbox developer account and search tweets: full archive sandbox access.

I loaded the packages this way:

r = getOption("repos")
r["CRAN"] = "http://cran.us.r-project.org"
options(repos = r)

ipak <- function(pkg){
    new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
    if (length(new.pkg)) 
        install.packages(new.pkg, dependencies = TRUE)
    sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("rtweet","dplyr", "tidyr", "tidyverse", "tidytext", "tm", "tmap", "SnowballC", "wordcloud", "RColorBrewer", "textdata", "bing", "reshape2")
ipak(packages)

I saved the details necessary to create the token and here is the code I used to create it (without publishing the private details of my token):

token <- create_token(
  app = appname,
  consumer_key = api_key,
  consumer_secret = api_secret_key,
  access_token = access_key, 
  access_secret = access_secret_key)

After setting up the environment, I ran the code to search for tweets but ran into an error. Below is the code I used to search for my tweets.

marriott <- rtweet::search_fullarchive(q = "marriott artificial intelligence", 
                               n = 100, 
                               fromDate = "201801010000", 
                               toDate = "20210600000", 
                               env_name = "pulltweets", 
                               safedir = NULL, 
                               parse = TRUE, 
                               token = token)

Here are further details of the error.

Error: max_id must be a character vector or data frame

Upon running 'rlang::last_error()', this was returned:

<error/rlang_error>
`max_id` must be a character vector or data frame
Backtrace:
 1. rtweet::search_fullarchive(...)
 2. rtweet:::search_premium(...)
 3. rtweet:::TWIT_paginate_max_id(...)
 4. rtweet::max_id(max_id)
 5. rtweet:::find_id(x, "max_id")

I ran a similar search using rtweet::search_30day, replacing the start and end time with 201801010000 and 201801300000, and ran into the exact same error.

When I try providing a random max_id value, such as the one shown below, I get another error:

marriott <- rtweet::search_fullarchive(q = "marriott artificial intelligence", 
                               n = 100, 
                               fromDate = "201801010000", 
                               toDate = "20210600000", 
                               env_name = "pulltweets", 
                               safedir = NULL, 
                               parse = TRUE, 
                               max_id = 100,
                               token = token)

Error in rtweet::search_fullarchive(q = "marriott artificial intelligence", : unused argument (max_id = 100)

I am currently using package version '0.7.0' for rtweet. I am able to run other functions such as search_tweets normally without getting this error.

Does anyone know how to fix this issue?


Solution

  • I found a fix to the above problem by adding one line of code to the token creation process, as seen below:

    token <- create_token(
      app = appname,
      consumer_key = api_key,
      consumer_secret = api_secret_key,
      access_token = access_key, 
      access_secret = access_secret_key,
      set_renv = TRUE)
    

    If anyone else comes up with any other fixes, feel free to update!