RStudio: Only showing results of previous code

76 views Asked by At

I am working on a sentiment analysis on Twitter, using rtweet package on Rstudio.

At first, I wanted to get 1000 tweets from Twitter using this function from rtweet package which worked fine:

Onlinesc <- search_tweets("online schooling", n=1000, include_rts = FALSE, retryonratelimit = TRUE)
Onlinesc$text

Result on console

After that, no matter how many times I change the number of tweets I want to view, it sill displays the same 1000 tweets it displayed previously. I think it is saved somewhere in the program and is preventing the console from showing new results.

What I've tried:

  • Clearing the environment and history of Rstudio every time before I run the code
  • Creating a new R script
  • Restarting Rstudio and my computer
  • Reloading packages
  • I used a function from twitteR package to extract Tweets but ended up facing the same issue
  • Regenerated my Twitter developer tokens and updated my Twitter Oauth several times

I am using Rstudio Version 1.3.1093

Any help will be highly appreciated :)

1

There are 1 answers

1
Scott Richter On

I'm assuming this has to do with the retryonratelimit argument.

I ran your code for retrieving 1000 tweets and saw that the df it produced was 1408 tweets long. Keeping the code the same, but changing n to 500, it produced an identical dataframe of 1408 tweets instead of 500.

I don't know what's up with getting 999 tweets instead of 1000 in the first call of my code with retryonratelimit set to false, but my call specifying n=500 got me 500 tweets just fine.

Here's my code.

library(rtweet)

# Original code produces 1408 tweets, not 1000.
Onlinesc <- search_tweets("online schooling", n=1000, include_rts = FALSE, retryonratelimit = TRUE)
Onlinesc$text[1]
length(Onlinesc$text)

# What if we reduce the n to 500 instead of 1000? Nope, still 1408.
Onlinesc500 <- search_tweets("online schooling", n=500, include_rts = FALSE, retryonratelimit = TRUE)
Onlinesc500$text[1]
length(Onlinesc500$text)

# Are these two sets identical? Yep.
sum(Onlinesc$text == Onlinesc500$text) == length(Onlinesc$text)

# We can see the last 6 tweets are identical.
tail(Onlinesc$text)
tail(Onlinesc500$text)

# So what gives?
# Looks like both the 1000 and 500 tweet dfs have 1408 observations. Why not the appropriate n we specify?
# Could this be due to the retryonratelimit argument? Let's see by adjusting it to false.
F_Onlinesc <- search_tweets("online schooling", n=1000, include_rts = FALSE, retryonratelimit = FALSE)
length(F_Onlinesc$text) # Interesting. We've got 999 tweets instead of 1000.

F_Onlinesc500 <- search_tweets("online schooling", n=500, include_rts = FALSE, retryonratelimit = FALSE)
length(F_Onlinesc500$text) # 500. We've got the appropriate number of tweets from this call.

F_Onlinesc$text[1] == F_Onlinesc500$text[1] # The first tweet is identical, which is to be expected. We get the most recent ones first.
tail(F_Onlinesc$text) == tail(F_Onlinesc500$text) # The last six tweets are dissimilar, which is to be expected.