Reputation: 1
I have script I'm using to pull and classify tweets. It uses a CSV where I have a series of usernames and other info. However some of the usernames are inactive and result in an empty data frame. This crashes my script. Any advice on error handling? Basically if the "temp" <-get_all_tweets gets 0 tweets I need to "next". What I have below doesn't work.
for (i in 1:length(usernames))
{
#get tweets and subset to text, ID, creation (may drop ID)
rm(temp)
temp<- get_all_tweets(users = usernames[i], start_tweets = start_date[i], end_tweets = end_date[i], bearer_token = get_bearer(),file="test",data_path="Tweets",is_retweet=FALSE, is_reply=FALSE,n = 15000) %>% subset(select=c("text","id","created_at"))
#error control
if (!exists("temp")){next}
rest of script works
Upvotes: 0
Views: 139
Reputation: 160607
This is a good example of error-handling using try
or tryCatch
. (There are other methods for working with signals, but they get a bit ... circuitous :-)
Try this:
for (i in seq_along(usernames)) {
temp <- tryCatch(
get_all_tweets(
users = usernames[i], start_tweets = start_date[i],
end_tweets = end_date[i], bearer_token = get_bearer(),
file = "test", data_path = "Tweets", is_retweet = FALSE,
is_reply = FALSE, n = 15000),
error = function(e) e)
if (inherits(temp, "error")) {
warning("error retrieving data for ", sQuote(usernames[i], FALSE), ": ",
conditionMessage(temp), call. = FALSE)
# perhaps something more here?
} else {
temp <- subset(temp, select = c("text", "id", "created_at"))
# rest of your code here
}
}
This code has a simple if
statement branching on success of the call to get_all_tweets
. In this case, I choose to do nothing more if an error occurs. There are times when you may want to do something else (e.g., call again with different args, or use default-data), then one might do something like this instead:
for (i in seq_along(usernames)) {
temp <- tryCatch(
get_all_tweets(
users = usernames[i], start_tweets = start_date[i],
end_tweets = end_date[i], bearer_token = get_bearer(),
file = "test", data_path = "Tweets", is_retweet = FALSE,
is_reply = FALSE, n = 15000),
error = function(e) e)
if (inherits(temp, "error")) {
warning("error retrieving data for ", sQuote(usernames[i], FALSE), ": ",
conditionMessage(temp), call. = FALSE)
# assign to temp from some other method such that
# the rest of your code works
} else {
temp <- subset(temp, select = c("text", "id", "created_at"))
}
# rest of your code here
}
Upvotes: 1