rgtrendsr

Error with date using gtrendsR


Why the code below works just fine:

library(gtrendsR)

ch <- gconnect("usr@gmail.com", "psw")

query1 <- gtrends(query = c("Ludwig von Mises", "John Maynard Keynes", "Karl Marx"),
                 geo = c("BR"),
                 start_date = "2016-01-01",
                 end_date = "2016-03-31")

But if I change the date to:

query2 <- gtrends(query = c("Ludwig von Mises", "John Maynard Keynes", "Karl Marx"),
                 geo = c("BR"),
                 start_date = "2010-01-01",
                 end_date = "2016-03-31")

R trows an error:

Error in as.POSIXlt.character(x, tz, ...) : 
  character string is not in a standard unambiguous format

Solution

  • I live in Brazil. The package seems not to handle the time-zone correctly if I do:

    Sys.setenv(LANG = "en", TZ = 'GMT')
    

    before i run the code, it works.

    If I enter the debug the problem is on the function

    .processResults(resultsText, queryparams) 
    

    on the line

    weeks <- lapply(weeks, as.POSIXct, SIMPLIFY = FALSE)
    

    if I change to

    weeks <- lapply(weeks, as.POSIXct, SIMPLIFY = FALSE, tz = 'GMT')
    

    it also works.