From 6676889e640ee19128a65ebe64921d27453ad704 Mon Sep 17 00:00:00 2001 From: Philippe Massicotte Date: Sun, 18 Apr 2021 14:29:21 -0400 Subject: [PATCH 1/2] Do not validate the geo code on our side. Closes #377. Closes #364. Closes# 368. Closes #367. Closes #335. Closes #327. Closes #324. Closes #202. --- R/gtrends.R | 34 ++++++++++++++++++++++++---------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/R/gtrends.R b/R/gtrends.R index c4a117c..3efb41a 100755 --- a/R/gtrends.R +++ b/R/gtrends.R @@ -134,17 +134,31 @@ gtrends <- function( is.character(cookie_url) ) - - ## Check if valide geo - if (geo != "" && - !all(geo %in% c(as.character(countries[, "country_code"]), as.character(countries[, "sub_code"])))) { - stop( - "Country code not valid. Please use 'data(countries)' to retreive valid codes.", - call. = FALSE - ) + ## Check if valid geo. There are no official source(s) that we can use to + ## validate the entered geo code. However, we can use a regular expression to + ## verify if the structure is valid. + + m <- regexpr("^[a-zA-Z]{2}((?:-\\w{1,3}))?(?:-\\d{1,3})?", geo) + ret <- regmatches(geo, m) + + if (all(geo != "")) { + if (!all.equal(ret, geo)) { + stop("Country code not formatted correctly.", call. = FALSE) + } } - - ## Check if valide category + + # if (geo != "" && + # !all(geo %in% + # c( + # as.character(countries[, "country_code"]), + # as.character(countries[, "sub_code"]) + # ))) { + # stop("Country code not valid. Please use 'data(countries)' to retrieve valid codes.", + # call. = FALSE + # ) + # } + + ## Check if valid category if (!all(category %in% categories[, "id"])) { stop( "Category code not valid. Please use 'data(categories)' to retreive valid codes.", From bf34a1bef4820bf60e3a3939fc9e14dab4fa96e4 Mon Sep 17 00:00:00 2001 From: Philippe Massicotte Date: Sun, 18 Apr 2021 14:35:57 -0400 Subject: [PATCH 2/2] Use identical() instead of all.equal(). --- R/gtrends.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/gtrends.R b/R/gtrends.R index 3efb41a..3599421 100755 --- a/R/gtrends.R +++ b/R/gtrends.R @@ -142,7 +142,7 @@ gtrends <- function( ret <- regmatches(geo, m) if (all(geo != "")) { - if (!all.equal(ret, geo)) { + if (!identical(ret, geo)) { stop("Country code not formatted correctly.", call. = FALSE) } }