PMassicotte/gtrendsR

Error in start_top:end_top : NA/NaN argument

Closed this issue Β· 11 comments

The following code:

plot(
  gtrends(
    keyword = "Coca-Cola",
    geo = "US",
    time = "2005-02-04 2005-02-05",
    onlyInterest = FALSE
  )
)

produces this error
Error in start_top:end_top : NA/NaN argument

also happens with this code (just to show that it's not about the 1 day difference)

gtrends(
  keyword = "Coca-Cola",
  geo = "US",
  time = "2005-02-04 2005-02-08"
)

Try with the dev version.

This is because you are looking at only one day (February 4, 2005 to February 5, 2005). If you look at google you will not get related topics for one day.

In fact, you will get the top related topics, but not the rising ones. Since the rising topics are empty. gtrends() will throw an error.

https://trends.google.com/trends/explore?date=2005-02-04%202005-02-05&geo=US&q=Coca%20Cola&hl=en

Try with the dev version.

I tried, didn't help

This is because you are looking at only one day (February 4, 2005 to February 5, 2005). If you look at google you will not get related topics for one day.

Ok, but still on the other example we look at 5 days (Feb 4 to Feb Feb 8) and the issue still occurs
this is the output from google for those dates:
https://trends.google.com/trends/explore?date=2005-02-04%202005-02-08&geo=US&q=Coca%20Cola&hl=en

That is not the request you included. (Probably my bad) You were looking for "coca-cola" not "coca cola". For coca-cola you do not have a rising related topic tab in the result from google. Have a look:
https://trends.google.com/trends/explore?date=2005-02-04%202005-02-08&geo=US&q=Coca-Cola&hl=en

hmmm, ok thanks for the enlightenment :)
btw, is there any chance to "fix" it? I mean either return the data (with zeros of course) or at least add something that is a little bit clearer in the error prompt

That is not the request you included. (Probably my bad) You were looking for "coca-cola" not "coca cola". For coca-cola you do not have a rising related topic tab in the result from google. Have a look: https://trends.google.com/trends/explore?date=2005-02-04%202005-02-08&geo=US&q=Coca-Cola&hl=en

Ok now I found something else (basically the same issue) :P , the following code

d2 <- gtrends("Coca Cola", geo = "US", time="2005-02-05 2005-02-07")
plot(d2)

yields the the same error but as you can see the results from google are perfectly fine
https://trends.google.com/trends/explore?date=2005-02-05%202005-02-07&geo=US&q=Coca%20Cola&hl=en

Like I said, works fine with the dev version

devtools::install_github("pmassicotte/gtrendsR", force = TRUE)
#> Using github PAT from envvar GITHUB_PAT
#> Downloading GitHub repo pmassicotte/gtrendsR@HEAD
#> 
#> ── R CMD build ─────────────────────────────────────────────────────────────────
#> * checking for file β€˜/tmp/RtmpWRSOnb/remotes1175efa5bb750/PMassicotte-gtrendsR-d53b9b7/DESCRIPTION’ ... OK
#> * preparing β€˜gtrendsR’:
#> * checking DESCRIPTION meta-information ... OK
#> * checking for LF line-endings in source and make files and shell scripts
#> * checking for empty or unneeded directories
#> * building β€˜gtrendsR_1.5.1.9000.tar.gz’
#> Installing package into '/home/filoche/R/x86_64-pc-linux-gnu-library/4.3'
#> (as 'lib' is unspecified)

library(gtrendsR)

d2 <- gtrends("Coca Cola", geo = "US", time = "2005-02-05 2005-02-07")
plot(d2)

Created on 2023-05-31 with reprex v2.0.2

Like I said, works fine with the dev version

devtools::install_github("pmassicotte/gtrendsR", force = TRUE)
#> Using github PAT from envvar GITHUB_PAT
#> Downloading GitHub repo pmassicotte/gtrendsR@HEAD
#> 
#> ── R CMD build ─────────────────────────────────────────────────────────────────
#> * checking for file β€˜/tmp/RtmpWRSOnb/remotes1175efa5bb750/PMassicotte-gtrendsR-d53b9b7/DESCRIPTION’ ... OK
#> * preparing β€˜gtrendsR’:
#> * checking DESCRIPTION meta-information ... OK
#> * checking for LF line-endings in source and make files and shell scripts
#> * checking for empty or unneeded directories
#> * building β€˜gtrendsR_1.5.1.9000.tar.gz’
#> Installing package into '/home/filoche/R/x86_64-pc-linux-gnu-library/4.3'
#> (as 'lib' is unspecified)

library(gtrendsR)

d2 <- gtrends("Coca Cola", geo = "US", time = "2005-02-05 2005-02-07")
plot(d2)

Created on 2023-05-31 with reprex v2.0.2

First, it's still not correct, there are differences between the values you receive and and what google shows if you'll look again at the google trands you will notice these differences :

your plot google's plot
x(Feb 05) < 87.5 x(Feb 05) = 91
x(Feb 06) = 0 x(Feb 06) = 25

Second, I've installed the dev version and still get the same, you can see here:
image
Also, I reinstalled R and R studio and updated all the packages just to see if it will work and it didn't (both on my PC and laptop)

Just a thought: Maybe your timezone matters / is different between web-browsing and API requests and leads to different cutoff times. You could try overriding that. This should wash out more if you compare 7 days or a month or longer.

I am also getting the same error for a longer time period (default of today+5-y):

gtrendsR::gtrends("internalpaint", geo = "GB")
#> Error in start_top:end_top: NA/NaN argument
devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16 ucrt)
#>  os       Windows 10 x64 (build 18362)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United Kingdom.utf8
#>  ctype    English_United Kingdom.utf8
#>  tz       Europe/Madrid
#>  date     2023-11-23
#>  pandoc   3.1.8 @ C:/Users/ALBERT~1.AGU/AppData/Local/Pandoc/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version date (UTC) lib source
#>  cachem        1.0.8   2023-05-01 [1] CRAN (R 4.3.1)
#>  callr         3.7.3   2022-11-02 [1] CRAN (R 4.3.1)
#>  cli           3.6.1   2023-03-23 [1] CRAN (R 4.3.1)
#>  colorspace    2.1-0   2023-01-23 [1] CRAN (R 4.3.1)
#>  crayon        1.5.2   2022-09-29 [1] CRAN (R 4.3.1)
#>  curl          5.0.2   2023-08-14 [1] CRAN (R 4.3.1)
#>  devtools      2.4.5   2022-10-11 [1] CRAN (R 4.3.1)
#>  digest        0.6.33  2023-07-07 [1] CRAN (R 4.3.1)
#>  dplyr         1.1.3   2023-09-03 [1] CRAN (R 4.3.1)
#>  ellipsis      0.3.2   2021-04-29 [1] CRAN (R 4.3.1)
#>  evaluate      0.22    2023-09-29 [1] CRAN (R 4.3.1)
#>  fansi         1.0.4   2023-01-22 [1] CRAN (R 4.3.1)
#>  fastmap       1.1.1   2023-02-24 [1] CRAN (R 4.3.1)
#>  fs            1.6.3   2023-07-20 [1] CRAN (R 4.3.1)
#>  generics      0.1.3   2022-07-05 [1] CRAN (R 4.3.1)
#>  ggplot2       3.4.3   2023-08-14 [1] CRAN (R 4.3.1)
#>  glue          1.6.2   2022-02-24 [1] CRAN (R 4.3.1)
#>  gtable        0.3.4   2023-08-21 [1] CRAN (R 4.3.1)
#>  gtrendsR      1.5.1   2022-05-23 [1] CRAN (R 4.3.2)
#>  htmltools     0.5.6   2023-08-10 [1] CRAN (R 4.3.1)
#>  htmlwidgets   1.6.2   2023-03-17 [1] CRAN (R 4.3.1)
#>  httpuv        1.6.11  2023-05-11 [1] CRAN (R 4.3.1)
#>  jsonlite      1.8.7   2023-06-29 [1] CRAN (R 4.3.1)
#>  knitr         1.44    2023-09-11 [1] CRAN (R 4.3.1)
#>  later         1.3.1   2023-05-02 [1] CRAN (R 4.3.1)
#>  lifecycle     1.0.3   2022-10-07 [1] CRAN (R 4.3.1)
#>  magrittr      2.0.3   2022-03-30 [1] CRAN (R 4.3.1)
#>  memoise       2.0.1   2021-11-26 [1] CRAN (R 4.3.1)
#>  mime          0.12    2021-09-28 [1] CRAN (R 4.3.0)
#>  miniUI        0.1.1.1 2018-05-18 [1] CRAN (R 4.3.1)
#>  munsell       0.5.0   2018-06-12 [1] CRAN (R 4.3.1)
#>  pillar        1.9.0   2023-03-22 [1] CRAN (R 4.3.1)
#>  pkgbuild      1.4.2   2023-06-26 [1] CRAN (R 4.3.1)
#>  pkgconfig     2.0.3   2019-09-22 [1] CRAN (R 4.3.1)
#>  pkgload       1.3.3   2023-09-22 [1] CRAN (R 4.3.1)
#>  prettyunits   1.2.0   2023-09-24 [1] CRAN (R 4.3.1)
#>  processx      3.8.2   2023-06-30 [1] CRAN (R 4.3.1)
#>  profvis       0.3.8   2023-05-02 [1] CRAN (R 4.3.1)
#>  promises      1.2.1   2023-08-10 [1] CRAN (R 4.3.1)
#>  ps            1.7.5   2023-04-18 [1] CRAN (R 4.3.1)
#>  purrr         1.0.2   2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache       0.16.0  2022-07-21 [1] CRAN (R 4.3.1)
#>  R.methodsS3   1.8.2   2022-06-13 [1] CRAN (R 4.3.0)
#>  R.oo          1.25.0  2022-06-12 [1] CRAN (R 4.3.0)
#>  R.utils       2.12.2  2022-11-11 [1] CRAN (R 4.3.1)
#>  R6            2.5.1   2021-08-19 [1] CRAN (R 4.3.1)
#>  Rcpp          1.0.11  2023-07-06 [1] CRAN (R 4.3.1)
#>  remotes       2.4.2.1 2023-07-18 [1] CRAN (R 4.3.1)
#>  reprex        2.0.2   2022-08-17 [1] CRAN (R 4.3.1)
#>  rlang         1.1.1   2023-04-28 [1] CRAN (R 4.3.1)
#>  rmarkdown     2.25    2023-09-18 [1] CRAN (R 4.3.1)
#>  scales        1.2.1   2022-08-20 [1] CRAN (R 4.3.1)
#>  sessioninfo   1.2.2   2021-12-06 [1] CRAN (R 4.3.1)
#>  shiny         1.7.5   2023-08-12 [1] CRAN (R 4.3.1)
#>  stringi       1.7.12  2023-01-11 [1] CRAN (R 4.3.0)
#>  stringr       1.5.0   2022-12-02 [1] CRAN (R 4.3.1)
#>  styler        1.10.2  2023-08-29 [1] CRAN (R 4.3.1)
#>  tibble        3.2.1   2023-03-20 [1] CRAN (R 4.3.1)
#>  tidyselect    1.2.0   2022-10-10 [1] CRAN (R 4.3.1)
#>  urlchecker    1.0.1   2021-11-30 [1] CRAN (R 4.3.1)
#>  usethis       2.2.2   2023-07-06 [1] CRAN (R 4.3.1)
#>  utf8          1.2.3   2023-01-31 [1] CRAN (R 4.3.1)
#>  vctrs         0.6.3   2023-06-14 [1] CRAN (R 4.3.1)
#>  withr         2.5.1   2023-09-26 [1] CRAN (R 4.3.1)
#>  xfun          0.40    2023-08-09 [1] CRAN (R 4.3.1)
#>  xtable        1.8-4   2019-04-21 [1] CRAN (R 4.3.1)
#>  yaml          2.3.7   2023-01-23 [1] CRAN (R 4.3.0)
#> 
#>  [1] C:/Program Files/R/R-4.3.1/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Created on 2023-11-23 with reprex v2.0.2