Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to pass column names to tidygeocoder::geocode() with wrapper function #189

Open
elipousson opened this issue May 1, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@elipousson
Copy link

I'm not sure whether this is a bug report or feature request but, given the way tidygeocoder::geocode() handles the evaluation of argument names, I can't figure out how to pass a variable. If there is a way to do this with rlang that doesn't require changes to tidygeocoder, I'd welcome any advice you can share. If not, I'd love to see an option that allows tidygeocoder::geocode() to work with this type of wrapper function.

tbl <- tibble::as_tibble(
  data.frame(
    "Addr" = "1 N. Charles St, Baltimore, MD 21201"
  )
)

test_geocode <- function(.tbl, address, ...) {
  tidygeocoder::geocode(
    .tbl = .tbl,
    address = address,
    ...
  )
}

test_geocode(tbl, "Addr")
#> Error: "address" is not a column name in the input dataset.

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


─ Session info ─────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.2.3 (2023-03-15)
 os       macOS Big Sur 11.7.1
 system   aarch64, darwin20
 ui       RStudio
 language (EN)
 collate  en_US.UTF-8
 ctype    en_US.UTF-8
 tz       America/New_York
 date     2023-05-01
 rstudio  2023.05.0-daily+191 Mountain Hydrangea (desktop)
 pandoc   3.1.1 @ /Applications/RStudio.app/Contents/Resources/app/quarto/bin/tools/ (via rmarkdown)

─ Packages ─────────────────────────────────────────────────────────────────────────────────
 ! package       * version    date (UTC) lib source
   assertthat      0.2.1      2019-03-21 [2] CRAN (R 4.2.0)
   brew            1.0-8      2022-09-29 [1] CRAN (R 4.2.0)
   brio            1.1.3      2021-11-30 [1] CRAN (R 4.2.0)
   cachem          1.0.7      2023-02-24 [1] CRAN (R 4.2.0)
   callr           3.7.3      2022-11-02 [1] CRAN (R 4.2.0)
   cellranger      1.1.0      2016-07-27 [2] CRAN (R 4.2.0)
   class           7.3-21     2023-01-23 [2] CRAN (R 4.2.3)
   classInt        0.4-9      2023-02-28 [1] CRAN (R 4.2.0)
   cli             3.6.1      2023-03-23 [1] CRAN (R 4.2.0)
   cliExtras       0.1.0.9001 2023-04-29 [1] Github (elipousson/cliExtras@a494ed3)
   clipr           0.8.0      2022-02-22 [1] CRAN (R 4.2.0)
   commonmark      1.9.0      2023-03-17 [1] CRAN (R 4.2.0)
   crayon          1.5.2      2022-09-29 [1] CRAN (R 4.2.0)
   crul            1.3        2022-09-03 [2] CRAN (R 4.2.0)
   curl            5.0.0      2023-01-12 [1] CRAN (R 4.2.0)
   datapasta       3.1.0      2020-01-17 [2] CRAN (R 4.2.0)
   DBI             1.1.3      2022-06-18 [1] CRAN (R 4.2.0)
   desc            1.4.2      2022-09-08 [1] CRAN (R 4.2.0)
   devtools      * 2.4.5      2022-10-11 [1] CRAN (R 4.2.0)
   digest          0.6.31     2022-12-11 [1] CRAN (R 4.2.0)
   dplyr           1.1.1      2023-03-22 [1] CRAN (R 4.2.0)
   e1071           1.7-13     2023-02-01 [1] CRAN (R 4.2.0)
   ellipsis        0.3.2      2021-04-29 [1] CRAN (R 4.2.0)
   esri2sf         0.1.1      2023-04-29 [1] Github (elipousson/esri2sf@44e76f4)
   evaluate        0.20       2023-01-17 [1] CRAN (R 4.2.0)
   fansi           1.0.4      2023-01-22 [1] CRAN (R 4.2.0)
   fastmap         1.1.1      2023-02-24 [1] CRAN (R 4.2.0)
   filenamr        0.1.0.9002 2023-04-06 [1] Github (elipousson/filenamr@84a2f15)
   fs              1.6.1      2023-02-06 [1] CRAN (R 4.2.0)
   gargle          1.3.0      2023-01-30 [2] CRAN (R 4.2.0)
   generics        0.1.3      2022-07-05 [1] CRAN (R 4.2.0)
   gistr           0.9.0      2020-07-29 [2] CRAN (R 4.2.0)
   glue            1.6.2      2022-02-24 [1] CRAN (R 4.2.0)
   googledrive     2.0.0      2021-07-08 [2] CRAN (R 4.2.0)
   googlesheets4   1.0.1      2022-08-13 [2] CRAN (R 4.2.0)
   hms             1.1.3      2023-03-21 [1] CRAN (R 4.2.0)
   htmltools       0.5.5      2023-03-23 [1] CRAN (R 4.2.0)
   htmlwidgets     1.6.2      2023-03-17 [1] CRAN (R 4.2.0)
   httpcode        0.3.0      2020-04-10 [2] CRAN (R 4.2.0)
   httpuv          1.6.9      2023-02-14 [1] CRAN (R 4.2.0)
   httr            1.4.5      2023-02-24 [1] CRAN (R 4.2.0)
   httr2           0.2.2      2022-09-25 [1] CRAN (R 4.2.0)
   jsonlite        1.8.4      2022-12-06 [1] CRAN (R 4.2.0)
   KernSmooth      2.23-20    2021-05-03 [2] CRAN (R 4.2.3)
   knitr           1.42       2023-01-25 [1] CRAN (R 4.2.0)
   later           1.3.0      2021-08-18 [1] CRAN (R 4.2.0)
   lifecycle       1.0.3      2022-10-07 [1] CRAN (R 4.2.0)
   magrittr        2.0.3      2022-03-30 [1] CRAN (R 4.2.0)
   memoise         2.0.1      2021-11-26 [1] CRAN (R 4.2.0)
   mime            0.12       2021-09-28 [1] CRAN (R 4.2.0)
   miniUI          0.1.1.1    2018-05-18 [1] CRAN (R 4.2.0)
   pak           * 0.4.0      2023-01-16 [1] local
   pillar          1.9.0      2023-03-22 [1] CRAN (R 4.2.0)
   pkgbuild        1.4.0      2022-11-27 [1] CRAN (R 4.2.0)
   pkgconfig       2.0.3      2019-09-22 [1] CRAN (R 4.2.0)
   pkgload         1.3.2      2022-11-16 [1] CRAN (R 4.2.0)
   prettyunits     1.1.1      2020-01-24 [1] CRAN (R 4.2.0)
   processx        3.8.0      2022-10-26 [1] CRAN (R 4.2.0)
   profvis         0.3.7      2020-11-02 [1] CRAN (R 4.2.0)
   progress        1.2.2      2019-05-16 [1] CRAN (R 4.2.0)
   promises        1.2.0.1    2021-02-11 [1] CRAN (R 4.2.0)
   proxy           0.4-27     2022-06-09 [1] CRAN (R 4.2.0)
   ps              1.7.4      2023-04-02 [1] CRAN (R 4.2.0)
   purrr           1.0.1      2023-01-10 [1] CRAN (R 4.2.0)
   R.cache         0.16.0     2022-07-21 [1] CRAN (R 4.2.0)
   R.methodsS3     1.8.2      2022-06-13 [1] CRAN (R 4.2.0)
   R.oo            1.25.0     2022-06-12 [1] CRAN (R 4.2.0)
   R.utils         2.12.2     2022-11-11 [1] CRAN (R 4.2.0)
   R6              2.5.1      2021-08-19 [1] CRAN (R 4.2.0)
   rappdirs        0.3.3      2021-01-31 [1] CRAN (R 4.2.0)
   Rcpp            1.0.10     2023-01-22 [1] CRAN (R 4.2.0)
   readr           2.1.4      2023-02-10 [1] CRAN (R 4.2.0)
   readxl          1.4.2      2023-02-09 [2] CRAN (R 4.2.0)
   rematch2        2.1.2      2020-05-01 [1] CRAN (R 4.2.0)
   remotes         2.4.2      2021-11-30 [1] CRAN (R 4.2.0)
   reprex          2.0.2      2022-08-17 [2] CRAN (R 4.2.0)
   rlang           1.1.0.9000 2023-04-15 [1] Github (r-lib/rlang@88352d9)
   rmarkdown       2.21       2023-03-26 [1] CRAN (R 4.2.0)
   roxygen2        7.2.3      2022-12-08 [1] CRAN (R 4.2.0)
   rprojroot       2.0.3      2022-04-02 [1] CRAN (R 4.2.0)
   rstudioapi      0.14       2022-08-22 [1] CRAN (R 4.2.0)
   sessioninfo     1.2.2      2021-12-06 [1] CRAN (R 4.2.0)
   sf              1.0-12     2023-03-19 [1] CRAN (R 4.2.0)
 P sfext         * 0.1.1      2023-04-29 [?] Github (elipousson/sfext@47ab9d5)
   shiny           1.7.4      2022-12-15 [1] CRAN (R 4.2.0)
   sinew         * 0.4.0      2023-04-05 [1] Github (yonicd/sinew@3d4acbb)
   sos             2.1-4      2022-04-01 [1] CRAN (R 4.2.0)
   stringi         1.7.12     2023-01-11 [1] CRAN (R 4.2.0)
   stringr         1.5.0      2022-12-02 [1] CRAN (R 4.2.0)
   styler          1.9.1      2023-03-04 [1] CRAN (R 4.2.0)
   testthat        3.1.7      2023-03-12 [1] CRAN (R 4.2.0)
   tibble          3.2.1      2023-03-20 [1] CRAN (R 4.2.0)
   tidygeocoder    1.0.5      2021-11-02 [2] CRAN (R 4.2.0)
   tidyselect      1.2.0      2022-10-10 [1] CRAN (R 4.2.0)
   tzdb            0.3.0      2022-03-28 [1] CRAN (R 4.2.0)
   units           0.8-2      2023-04-27 [1] CRAN (R 4.2.0)
   urlchecker      1.0.1      2021-11-30 [1] CRAN (R 4.2.0)
   usethis       * 2.1.6.9000 2023-04-06 [1] Github (r-lib/usethis@04ebe9e)
   utf8            1.2.3      2023-01-31 [1] CRAN (R 4.2.0)
   vctrs           0.6.2      2023-04-19 [1] CRAN (R 4.2.0)
   withr           2.5.0      2022-03-03 [1] CRAN (R 4.2.0)
   xfun            0.38       2023-03-24 [1] CRAN (R 4.2.0)
   xml2            1.3.3      2021-11-30 [1] CRAN (R 4.2.0)
   xtable          1.8-4      2019-04-21 [1] CRAN (R 4.2.0)
   yaml            2.3.7      2023-01-23 [1] CRAN (R 4.2.0)

 [1] /Users/elipousson/Library/R/arm64/4.2/library
 [2] /Library/Frameworks/R.framework/Versions/4.2-arm64/Resources/library

 P ── Loaded and on-disk path mismatch.

@elipousson elipousson added the bug Something isn't working label May 1, 2023
@jessecambon
Copy link
Owner

jessecambon commented May 1, 2023

Hi @elipousson, there might be a more elegant solution than this but if you are OK using named arguments then this works for me:

tbl <- tibble::as_tibble(
  data.frame(
    "Addr" = "1 N. Charles St, Baltimore, MD 21201"
  )
)

test_geocode <- function(.tbl, ...) {
  tidygeocoder::geocode(
    .tbl = .tbl,
    ...
  )
}

test_geocode(tbl, address="Addr")
#> Passing 1 address to the Nominatim single address geocoder
#> Query completed in: 1 seconds
#> # A tibble: 1 × 3
#>   Addr                                   lat  long
#>   <chr>                                <dbl> <dbl>
#> 1 1 N. Charles St, Baltimore, MD 21201  39.3 -76.6

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

I'd have to do a little more digging to figure out why your original code is behaving that way, but I suspect it has to do with how the arguments are processed in geocode(). There's some logic here that allows the address argument to be passed in either quoted or unquoted form (ie. address = "colname" and address = colname both work):

if (!is.null(substitute(address))) address <- rm_quote(deparse(substitute(address)))

@elipousson
Copy link
Author

I think the issue is the way that substitute() is dependent on the local environment. For example, running the helper function with withr::local_environment() gets the code to pass the initial check for column names – although it returns a different error for unknown reasons. I forked tidygeocoder to explore possible alternatives and tried out a tidyselect-based helper but it alerted me that tidyselect no longer supports character strings for column selection (potentially due to this kind of quirky issue). There is more information in this tidyselect vignette if you're curious.

tbl <- tibble::as_tibble(
  data.frame(
    "Addr" = "1 N. Charles St, Baltimore, MD 21201"
  )
)

test_geocode <- function(.tbl, ...) {
  withr::local_environment(
    parent.frame(),
    tidygeocoder::geocode(
      .tbl = .tbl,
      ...,
      
    )
  )
}

test_geocode(tbl, "Addr")
#> Passing 1 address to the Nominatim single address geocoder
#> Query completed in: 1 seconds
#> Error in if (pos == 1L) stop("'pos=1' is not possible and has been warned about for years"): the condition has length > 1

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants