首页 > 解决方案 > RStudio:仅显示以前代码的结果

问题描述

我正在使用 Rstudio 上的 rtweet 包在 Twitter 上进行情绪分析。

起初,我想使用 rtweet 包中的这个函数从 Twitter 获取 1000 条推文,效果很好:

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

控制台上的结果

之后,无论我更改要查看的推文数量多少次,它仍然会显示与之前显示的相同的 1000 条推文。我认为它保存在程序中的某个位置,并阻止控制台显示新结果。

我试过的:

我正在使用 Rstudio 版本 1.3.1093

任何帮助将不胜感激:)

标签: rtwitter-oauthsentiment-analysisrtweet

解决方案


我假设这与 retryonratelimit 参数有关。

我运行你的代码来检索 1000 条推文,发现它产生的 df 有 1408 条推文。保持代码相同,但将 n 更改为 500,它生成了相同的数据帧,包含 1408 条推文,而不是 500 条。

我不知道在 retryonratelimit 设置为 false 的代码的第一次调用中获得 999 条推文而不是 1000 条是怎么回事,但是我指定 n=500 的调用让我得到 500 条推文就好了。

这是我的代码。

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. 

推荐阅读