From edb6165613051ad32a0fec6a5237d84da88926fd Mon Sep 17 00:00:00 2001 From: Jean-Marie Pivette Date: Sun, 24 Jan 2021 18:44:47 +0100 Subject: [PATCH 1/7] Add argument to geo_code() --- R/geocode.R | 24 +++++++++++++++++++++--- man/geocode.Rd | 15 +++++++++++++-- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/R/geocode.R b/R/geocode.R index 3abfa92..dfeb333 100644 --- a/R/geocode.R +++ b/R/geocode.R @@ -50,7 +50,10 @@ get_features <- function(x) { #' Geocode #' -#' @param query a string of the adress you want to geocode +#' @param query a string of the address you want to geocode +#' @param limit an integer maximum number of results +#' @param autocomplete a boolean +#' @param type of result wanted. Default returns all results #' #' @return a tibble #' @export @@ -58,9 +61,24 @@ get_features <- function(x) { #' @examples #' geocode(query = "39 quai André Citroën, Paris") #' -geocode <- function(query) { +geocode <- function(query, + limit = NULL, + autocomplete = NULL, + type = c("all","housenumber", "street", "locality", "municipality")) { + + if (missing(type) || type == "all") + type <- NULL + base_url <- "http://api-adresse.data.gouv.fr/search/?q=" - get_query <- httr::GET(utils::URLencode(paste0(base_url, query))) + get_query <- httr::GET( + url = base_url, + query = list( + q = query, + limit = limit, + autocomplete = as.integer(autocomplete), + type = type + ) + ) message( httr::status_code(get_query) ) diff --git a/man/geocode.Rd b/man/geocode.Rd index 934ca8f..ef62c0f 100644 --- a/man/geocode.Rd +++ b/man/geocode.Rd @@ -4,10 +4,21 @@ \alias{geocode} \title{Geocode} \usage{ -geocode(query) +geocode( + query, + limit = NULL, + autocomplete = NULL, + type = c("all", "housenumber", "street", "locality", "municipality") +) } \arguments{ -\item{query}{a string of the adress you want to geocode} +\item{query}{a string of the address you want to geocode} + +\item{limit}{an integer maximum number of results} + +\item{autocomplete}{a boolean} + +\item{type}{of result wanted. Default returns all results} } \value{ a tibble From d8f57135dadee2b9be369574434a03e2dd5ca3ad Mon Sep 17 00:00:00 2001 From: Jean-Marie Pivette Date: Sun, 24 Jan 2021 18:50:40 +0100 Subject: [PATCH 2/7] Add unit test on geocode() --- tests/testthat/test_geocode.R | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/testthat/test_geocode.R b/tests/testthat/test_geocode.R index d29ffe4..1a71883 100644 --- a/tests/testthat/test_geocode.R +++ b/tests/testthat/test_geocode.R @@ -9,6 +9,16 @@ test_that( } ) +test_that( + desc = "Geocode works with arguments", + code = { + perros_out <- geocode("perros", type = "municipality", limit = 3) + expect_s3_class(perros_out, "tbl_df") + expect_equal(nrow(perros_out), 3) + expect_true(all(perros_out$type %in% "municipality")) + } +) + test_that( desc = "Reverse geocode works", code = { From faf0807bb85e6b82a400b3b365a9d1e9a11ced35 Mon Sep 17 00:00:00 2001 From: Jean-Marie Pivette Date: Sun, 24 Jan 2021 21:25:57 +0100 Subject: [PATCH 3/7] fix warnings and notes during R CMD Check --- DESCRIPTION | 1 + NAMESPACE | 1 + R/geocode_tbl.R | 6 ++++-- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 625092e..8a95849 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -19,6 +19,7 @@ Imports: tibble, purrr, rlang, + stringr, utils Suggests: testthat, diff --git a/NAMESPACE b/NAMESPACE index f0d1860..733d0ff 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,3 +6,4 @@ export(geocode_tbl) export(reverse_geocode) export(reverse_geocode_tbl) importFrom(magrittr,"%>%") +importFrom(rlang,":=") diff --git a/R/geocode_tbl.R b/R/geocode_tbl.R index 0af67cb..f20b6fc 100644 --- a/R/geocode_tbl.R +++ b/R/geocode_tbl.R @@ -10,6 +10,8 @@ #' @return an augmented data frame of class tbl with latitude, longitude, etc #' @export #' +#' @importFrom rlang := +#' #' @examples #' #' table_test <- tibble::tibble( @@ -35,7 +37,7 @@ geocode_tbl <- function(tbl, adresse, code_insee = NULL, code_postal = NULL) { dplyr::select(.data = tbl, !!! vars) %>% dplyr::mutate({{adresse}} := stringr::str_replace({{adresse}}, "'", " ")) %>% - readr::write_csv(path = tmp) + readr::write_csv(file = tmp) message( "If file is larger than 8 MB, it must be splitted\n", @@ -134,7 +136,7 @@ reverse_geocode_tbl <- function(tbl, longitude, latitude) { "latitude" = rlang::enquo(latitude) ) dplyr::select(.data = tbl, !!! vars) %>% - readr::write_csv(path = tmp) + readr::write_csv(file = tmp) tbl_temp <- dplyr::select( .data = tbl, From 8825066615a4007b79fc36ca6afa20bad8483836 Mon Sep 17 00:00:00 2001 From: Jean-Marie Pivette Date: Sun, 24 Jan 2021 21:29:13 +0100 Subject: [PATCH 4/7] minor: remove trailing whitespaces --- R/geocode.R | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/R/geocode.R b/R/geocode.R index dfeb333..4bc3608 100644 --- a/R/geocode.R +++ b/R/geocode.R @@ -52,7 +52,7 @@ get_features <- function(x) { #' #' @param query a string of the address you want to geocode #' @param limit an integer maximum number of results -#' @param autocomplete a boolean +#' @param autocomplete a boolean #' @param type of result wanted. Default returns all results #' #' @return a tibble @@ -60,15 +60,15 @@ get_features <- function(x) { #' #' @examples #' geocode(query = "39 quai André Citroën, Paris") -#' -geocode <- function(query, +#' +geocode <- function(query, limit = NULL, autocomplete = NULL, - type = c("all","housenumber", "street", "locality", "municipality")) { - - if (missing(type) || type == "all") + type = c("all", "housenumber", "street", "locality", "municipality")) { + + if (missing(type) || type == "all") type <- NULL - + base_url <- "http://api-adresse.data.gouv.fr/search/?q=" get_query <- httr::GET( url = base_url, From 9fd2ed19c09165f14da124eab7c028c43d7f448c Mon Sep 17 00:00:00 2001 From: Jean-Marie Pivette Date: Sun, 24 Jan 2021 21:40:30 +0100 Subject: [PATCH 5/7] Add information to documentation --- R/geocode.R | 6 ++++-- man/geocode.Rd | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/R/geocode.R b/R/geocode.R index 4bc3608..47518db 100644 --- a/R/geocode.R +++ b/R/geocode.R @@ -51,9 +51,11 @@ get_features <- function(x) { #' Geocode #' #' @param query a string of the address you want to geocode -#' @param limit an integer maximum number of results +#' @param limit an integer indicating the maximum number of results wanted #' @param autocomplete a boolean -#' @param type of result wanted. Default returns all results +#' @param type a string with the type of result wanted. +#' Default returns all results. +#' Can have one of the following values: "all", "housenumber", "street", "locality" or "municipality" #' #' @return a tibble #' @export diff --git a/man/geocode.Rd b/man/geocode.Rd index ef62c0f..63c3c57 100644 --- a/man/geocode.Rd +++ b/man/geocode.Rd @@ -14,11 +14,13 @@ geocode( \arguments{ \item{query}{a string of the address you want to geocode} -\item{limit}{an integer maximum number of results} +\item{limit}{an integer indicating the maximum number of results wanted} \item{autocomplete}{a boolean} -\item{type}{of result wanted. Default returns all results} +\item{type}{a string with the type of result wanted. +Default returns all results. +Can have one of the following values: "all", "housenumber", "street", "locality" or "municipality"} } \value{ a tibble From 4baf6d415b3f72697f2232c935ba4dcb38c7a4b3 Mon Sep 17 00:00:00 2001 From: Jean-Marie Pivette Date: Sun, 24 Jan 2021 21:50:50 +0100 Subject: [PATCH 6/7] minor modification of base_url inside geocode() --- R/geocode.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/geocode.R b/R/geocode.R index 47518db..cdeb626 100644 --- a/R/geocode.R +++ b/R/geocode.R @@ -71,7 +71,7 @@ geocode <- function(query, if (missing(type) || type == "all") type <- NULL - base_url <- "http://api-adresse.data.gouv.fr/search/?q=" + base_url <- "http://api-adresse.data.gouv.fr/search/" get_query <- httr::GET( url = base_url, query = list( From fd08bd9a2ed0dd0c4422f6f6da0a7912351573e9 Mon Sep 17 00:00:00 2001 From: Jean-Marie Pivette Date: Tue, 16 Feb 2021 09:31:26 +0100 Subject: [PATCH 7/7] fix issue #33 --- R/geocode_tbl.R | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/geocode_tbl.R b/R/geocode_tbl.R index f20b6fc..0669f0b 100644 --- a/R/geocode_tbl.R +++ b/R/geocode_tbl.R @@ -37,7 +37,7 @@ geocode_tbl <- function(tbl, adresse, code_insee = NULL, code_postal = NULL) { dplyr::select(.data = tbl, !!! vars) %>% dplyr::mutate({{adresse}} := stringr::str_replace({{adresse}}, "'", " ")) %>% - readr::write_csv(file = tmp) + readr::write_csv(file = tmp, na = "") message( "If file is larger than 8 MB, it must be splitted\n",