DOI-USGS / hydroloom

hydrologic geospatial fabric creation tools. See official repository here: https://code.usgs.gov/water/hydroloom
https://doi-usgs.github.io/hydroloom/
Creative Commons Zero v1.0 Universal
25 stars 2 forks source link

Odd behavoir if `net` object in environment #35

Closed mikejohnson51 closed 2 months ago

mikejohnson51 commented 2 months ago

Hi @dblodgett-usgs here is an odd one! If a user has an object called net in the global environment, it causes many hydroloom functions to fail. Here is an example:

good!

(x <- sf::read_sf(system.file("extdata/new_hope.gpkg", package = "hydroloom")) |> 
  hydroloom::add_toids() |> 
  dplyr::select(id = COMID, toid) |>
  sf::st_drop_geometry())
#> # A tibble: 746 × 2
#>         id    toid
#>  *   <int>   <dbl>
#>  1 8893864 8894334
#>  2 8894490 8894336
#>  3 8894494 8894490
#> # ℹ 736 more rows

(x <- hydroloom::add_streamorder(x))
#> # A tibble: 746 × 4
#>        id    toid stream_order stream_calculator
#>     <int>   <dbl>        <dbl>             <dbl>
#> 1 8893864 8894334            4                 4
#> 2 8894490 8894336            4                 4
#> 3 8894494 8894490            4                 4
#> 4 8894334 8894492            4                 4
#> 5 8894492 8894494            4                 4
#> 6 8893850 8893864            4                 4

Created on 2024-06-27 with reprex v2.1.0

Fails

(x <- sf::read_sf(system.file("extdata/new_hope.gpkg", package = "hydroloom")) |> 
    hydroloom::add_toids() |> 
    dplyr::select(id = COMID, toid) |>
    sf::st_drop_geometry())
#> # A tibble: 746 × 2
#>         id    toid
#>  *   <int>   <dbl>
#>  1 8893864 8894334
#>  2 8894490 8894336
#>  3 8894494 8894490
#> # ℹ 736 more rows

net = data.frame()

x <- hydroloom::add_streamorder(x)
#> Error in `select()`:
#> ! Can't select columns that don't exist.
#> ✖ Column `id` doesn't exist.

Created on 2024-06-27 with reprex v2.1.0

Sometimes it fails with #> ✖ Column id doesn't exist., sometimes with #> ✖ Column toid doesn't exist. I cant find any rhyme or reason to that difference

dblodgett-usgs commented 2 months ago

Thanks for the report @mikejohnson51 -- this appears to be a bug in my use of exists -- I did not specify where parameter so it's looking in the global environment! Whoops!!!