DOI-USGS / dataRetrieval

This R package is designed to obtain USGS or EPA water quality sample data, streamflow data, and metadata directly from web services.
https://doi-usgs.github.io/dataRetrieval/
Other
260 stars 84 forks source link

Unexpected warning when providing duplicate pcodes to `pcode_to_name` #702

Closed jpadilla-spu closed 5 months ago

jpadilla-spu commented 5 months ago

Describe the bug

Runing pcode_to_name throws the following warning when you supply duplicate pcodes to the ParameterCd argument of pcode_to_name().

Here is the warning:

Caused by warning in `pcode_to_name()`:
! The following pCodes seem mistyped, and no information was returned:  

In the above warning, notice that the space after the base message is blank.

To Reproduce

The empty warning occurs when you include duplicate pcodes to the parameterCd argument. Here is the minimum reproducible example:

library(dataRetrival)

# works!
pcode_to_name(c("62614", "00060"))

# empty warning!
pcode_to_name(c("62614", "00060", "00060"))

Here is a reproducible example that contains a reasonable use case - adding a column that converts the pcode to a human readable name as part of a dplyr pipeline:

library(dataRetrieval)
library(tidyverse)
library(tidyr)

# SITE INVENTORY - USGS
usgs_of_interest <- tibble(
  site_name = c("chester morse lake", "masonry pool", "tolt reservoir"),
  site_no = c(12115000, 12116060, 12147900),
  pcode = c("00065", "00065", "62614")
)

# query instantaneous values
uv_values <- readNWISuv(
  siteNumbers = usgs_of_interest |> pull(site_no),
  parameterCd = usgs_of_interest |> pull(pcode),
  startDate = Sys.Date() - 1,
  ) |> 
  # pitch the flag columns
  select(- ends_with("_cd")) |>
  tidyr::pivot_longer(
    cols = contains("X_"),
    names_to = "pcode",
    values_to = "value"
  ) |>
  mutate(
    pcode = str_extract(pcode, "\\d{5}"),
    pcode_name = pcode_to_name(pcode)$characteristicname
  ) |> 
  na.omit()

Expected behavior I would expect pcode_to_name to skip the warning message even if I provide duplicate pcodes. Here is the line of code that produces the warning in dataRetrieval

Because I am using duplicate pCodes c("62614", "00060", "00060"), nrow(retval) != length(parameterCd) returns TRUE, but because all of the pcodes are valid the warning is empty. This function would work if I looked for the length of unique pcodes instead: if (nrow(retval) != length(unique(parameterCd))) {

Session Info

> devtools::session_info()
─ Session info ──────────────────────────────────────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.3.3 (2024-02-29 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_United States.utf8
 ctype    English_United States.utf8
 tz       America/Los_Angeles
 date     2024-04-17
 rstudio  2023.12.1+402 Ocean Storm (desktop)
 pandoc   NA

─ Packages ──────────────────────────────────────────────────────────────────────────────────────────────────
 package       * version date (UTC) lib source
 bit             4.0.5   2022-11-15 [1] CRAN (R 4.3.2)
 bit64           4.0.5   2020-08-30 [1] CRAN (R 4.3.2)
 cachem          1.0.8   2023-05-01 [1] CRAN (R 4.3.2)
 cellranger      1.1.0   2016-07-27 [1] CRAN (R 4.3.2)
 class           7.3-22  2023-05-03 [2] CRAN (R 4.3.3)
 classInt        0.4-10  2023-09-05 [1] CRAN (R 4.3.2)
 cli             3.6.2   2023-12-11 [1] CRAN (R 4.3.2)
 colorspace      2.1-0   2023-01-23 [1] CRAN (R 4.3.2)
 crayon          1.5.2   2022-09-29 [1] CRAN (R 4.3.2)
 curl            5.2.0   2023-12-08 [1] CRAN (R 4.3.2)
 dataRetrieval * 2.7.14  2023-10-05 [1] CRAN (R 4.3.2)
 DBI             1.1.3   2022-06-18 [1] CRAN (R 4.3.2)
 devtools        2.4.5   2022-10-11 [1] CRAN (R 4.3.2)
 digest          0.6.33  2023-07-07 [1] CRAN (R 4.3.2)
 dplyr         * 1.1.4   2023-11-17 [1] CRAN (R 4.3.2)
 e1071           1.7-14  2023-12-06 [1] CRAN (R 4.3.2)
 ellipsis        0.3.2   2021-04-29 [1] CRAN (R 4.3.2)
 fansi           1.0.6   2023-12-08 [1] CRAN (R 4.3.2)
 fastmap         1.1.1   2023-02-24 [1] CRAN (R 4.3.2)
 forcats       * 1.0.0   2023-01-29 [1] CRAN (R 4.3.2)
 fs              1.6.3   2023-07-20 [1] CRAN (R 4.3.2)
 generics        0.1.3   2022-07-05 [1] CRAN (R 4.3.2)
 ggplot2       * 3.4.4   2023-10-12 [1] CRAN (R 4.3.2)
 glue            1.6.2   2022-02-24 [1] CRAN (R 4.3.2)
 gtable          0.3.4   2023-08-21 [1] CRAN (R 4.3.2)
 hms             1.1.3   2023-03-21 [1] CRAN (R 4.3.2)
 htmltools       0.5.7   2023-11-03 [1] CRAN (R 4.3.2)
 htmlwidgets     1.6.4   2023-12-06 [1] CRAN (R 4.3.2)
 httpuv          1.6.13  2023-12-06 [1] CRAN (R 4.3.2)
 httr            1.4.7   2023-08-15 [1] CRAN (R 4.3.2)
 jsonlite        1.8.8   2023-12-04 [1] CRAN (R 4.3.2)
 KernSmooth      2.23-22 2023-07-10 [2] CRAN (R 4.3.3)
 later           1.3.2   2023-12-06 [1] CRAN (R 4.3.2)
 lifecycle       1.0.4   2023-11-07 [1] CRAN (R 4.3.2)
 lubridate     * 1.9.3   2023-09-27 [1] CRAN (R 4.3.2)
 magrittr        2.0.3   2022-03-30 [1] CRAN (R 4.3.2)
 memoise         2.0.1   2021-11-26 [1] CRAN (R 4.3.2)
 mime            0.12    2021-09-28 [1] CRAN (R 4.3.1)
 miniUI          0.1.1.1 2018-05-18 [1] CRAN (R 4.3.2)
 munsell         0.5.0   2018-06-12 [1] CRAN (R 4.3.2)
 openxlsx2     * 1.4     2024-02-15 [1] CRAN (R 4.3.2)
 pillar          1.9.0   2023-03-22 [1] CRAN (R 4.3.2)
 pkgbuild        1.4.3   2023-12-10 [1] CRAN (R 4.3.2)
 pkgconfig       2.0.3   2019-09-22 [1] CRAN (R 4.3.2)
 pkgload         1.3.3   2023-09-22 [1] CRAN (R 4.3.2)
 profvis         0.3.8   2023-05-02 [1] CRAN (R 4.3.2)
 promises        1.2.1   2023-08-10 [1] CRAN (R 4.3.2)
 proxy           0.4-27  2022-06-09 [1] CRAN (R 4.3.2)
 purrr         * 1.0.2   2023-08-10 [1] CRAN (R 4.3.2)
 R6              2.5.1   2021-08-19 [1] CRAN (R 4.3.2)
 Rcpp            1.0.11  2023-07-06 [1] CRAN (R 4.3.2)
 readr         * 2.1.4   2023-02-10 [1] CRAN (R 4.3.2)
 readxl        * 1.4.3   2023-07-06 [1] CRAN (R 4.3.2)
 rematch         2.0.0   2023-08-30 [1] CRAN (R 4.3.2)
 remotes         2.4.2.1 2023-07-18 [1] CRAN (R 4.3.2)
 rlang           1.1.2   2023-11-04 [1] CRAN (R 4.3.2)
 rstudioapi      0.15.0  2023-07-07 [1] CRAN (R 4.3.2)
 rvest           1.0.4   2024-02-12 [1] CRAN (R 4.3.2)
 scales          1.3.0   2023-11-28 [1] CRAN (R 4.3.2)
 selectr         0.4-2   2019-11-20 [1] CRAN (R 4.3.2)
 sessioninfo     1.2.2   2021-12-06 [1] CRAN (R 4.3.2)
 sf              1.0-15  2023-12-18 [1] CRAN (R 4.3.2)
 shiny           1.8.0   2023-11-17 [1] CRAN (R 4.3.2)
 snotelr       * 1.2     2023-09-16 [1] CRAN (R 4.3.2)
 stringi         1.8.3   2023-12-11 [1] CRAN (R 4.3.2)
 stringr       * 1.5.1   2023-11-14 [1] CRAN (R 4.3.2)
 sys           * 3.4.2   2023-05-23 [1] CRAN (R 4.3.3)
 tibble        * 3.2.1   2023-03-20 [1] CRAN (R 4.3.2)
 tidyr         * 1.3.0   2023-01-24 [1] CRAN (R 4.3.2)
 tidyselect      1.2.0   2022-10-10 [1] CRAN (R 4.3.2)
 tidyverse     * 2.0.0   2023-02-22 [1] CRAN (R 4.3.2)
 timechange      0.2.0   2023-01-11 [1] CRAN (R 4.3.2)
 tzdb            0.4.0   2023-05-12 [1] CRAN (R 4.3.2)
 units           0.8-5   2023-11-28 [1] CRAN (R 4.3.2)
 urlchecker      1.0.1   2021-11-30 [1] CRAN (R 4.3.2)
 usethis         2.2.2   2023-07-06 [1] CRAN (R 4.3.2)
 utf8            1.2.4   2023-10-22 [1] CRAN (R 4.3.2)
 vctrs           0.6.5   2023-12-01 [1] CRAN (R 4.3.2)
 vroom           1.6.5   2023-12-05 [1] CRAN (R 4.3.2)
 withr           2.5.2   2023-10-30 [1] CRAN (R 4.3.2)
 xml2            1.3.6   2023-12-04 [1] CRAN (R 4.3.2)
 xtable          1.8-4   2019-04-21 [1] CRAN (R 4.3.2)
 zip             2.3.0   2023-04-17 [1] CRAN (R 4.3.2)

 [1] ~path/obscured/R/win-library/4.3
 [2] ~path/obscured/R/R-4.3.3/library

Additional information

I intend to submit a PR that fixes the above problem