Reputation: 161
I'm running a list of strings though an API for an NLP processing. Sometimes the number of tokens is too short and the API returns an error. I've already excluded smaller strings, but sometimes there's an inconsistency (for example what I deem quickly to be a token is rejected by the API). There are only a few, but it's a very long list and I want to let it run through the night without checking on it every once in a while.
So that's why I need for the loop to continue even after an error.
So I already fixed the problem but it's not optimal, I used the try command to check if there is an error:
for(i in 1:nrow(df){
#First I've filtered out what content is clearly too short:
if(sapply(strsplit(df$Content[i], " "), length) > 19){
res <- try(temp_analysis <- gl_nlp(df$Content[i], language = "en"))
if(inherits(res, "try-error"))
{
next
}
temp_analysis <- gl_nlp(df$Content[i], language = "en")
And then some other stuff here
}
}
This works quite fine, but the problem is that it access the API twice and is thus slower and bills me twice.
So is there a way to get this same effect, but without using the try command? Or a derivative of this command that doesn't actually need to call on the API first?
Thanks in advance.
Upvotes: 2
Views: 185
Reputation: 132989
You don't need to access the API twice with try
. Here is a simple example:
res <- try(a)
#Error in try(a) : object 'a' not found
class(res)
#[1] "try-error"
a <- 1
res <- try(a)
res
#[1] 1
Upvotes: 5