'Using a for loop to get bulk tweets from the Twitter API v2 endpoint in R

I am trying to collect more tweets than is allowed in a single query, hence I am using a for loop to automate this.

tweets <- data_frame()

for(i in 1:10){
httr::GET(url = url_tweet,
        httr::add_headers(.headers = headers),
        query = params) %>%


httr::content(response, as = "text") %>%


fromJSON(obj, flatten = TRUE) %>%



json_data <- view(enframe(unlist(json_data))) %>%

mutate(
  id2 = name %>% str_extract("[0-9]+$"), # ensure unique rows
  name = name %>% str_remove("[0-9]+$") %>% str_remove("^data.")
) %>%
pivot_wider(names_from = name, values_from = value) %>%
select(`tweet_id` = id, text, user_id=includes.users.id, user_name=includes.users.username, likes=public_metrics.like_count, retweets=public_metrics.retweet_count, quotes=public_metrics.quote_count) %>%
type_convert() -> data_sep

tweets <- rbind(tweets, data_sep)
}

I have run the code individually and there is nothing wrong with any of it, but when I try to loop it I get this error

Error in `select()`:
! Can't subset columns that don't exist.
x Column `id` doesn't exist.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source