rloopsfor-loopgtrendsr

For loop skipping error


I would like get google trend data through a for loop. However, an error is holding me back. After searching through other stack questions I still cannot make it work. The loop in question:

a2p = for (i in dfurlnames$names1)
{ 
    x<- paste(i)
    gtrends_function3(x)
}

In my for loop I get the following error:

Error : res$status_code == 200 is not TRUE

I use the following packages and function:

Get the new gtrendsR; devtools::install_github('PMassicotte/gtrendsR')

library(gtrendsR)

gtrends_function3 <- function(x)
{
    trend1 = gtrends(c(x), geo = c(""), time = "2014-01-05 2014-10-04")
    trend_df1 = ldply(trend1)
    return(as.numeric(trend_df1$hits))        
}

The list:

dfurlnames$names1 = Ang babaeng humayo, The Bad Batch, Une vie, La La Land,               
The Light Between Oceans, El ciudadano ilustre, Spira Mirabilis, La región 
salvaje, Nocturnal Animals

Solution

  • Status code 200 refers to the HTTP protocol, indicating that everything went ok. Probably, you are requesting things too fast in the for loop. Add a sleep command, e.g.:

    Sys.sleep(1)

    in your for loop to slow things down. Alternatively, use a tryCatch to bypass:

    a2p = for (i in dfurlnames$names1)
    { 
        tryCatch({
           x<- paste(i)
           gtrends_function3(x)
        }, error=function(e) {print(e)})
    }