huizezhang-sherry / cubble

A tidy structure for spatio-temporal vector data
https://huizezhang-sherry.github.io/cubble/
Other
55 stars 9 forks source link

`unfold()` does not work for coords/columns called "x" #21

Closed loreabad6 closed 7 months ago

loreabad6 commented 8 months ago

Hi Sherry,

I have been playing with the package lately, it is really a great implementation and I am looking forward to contributing to it as much as possible, so consider this my first step 😀

I am working with projected data, which when creating a cubble_df result in the columns "x" and "y" as the "coords". However, when using unfold() I get an error for the column called "x".

I traced it back to this line in new_temporal_cubble() when assigning the classes. The backtrace leads me to tsibble:::validate_index(tbl, !!qindex) but that is as far as I got.

Here is a reproducible example:

library(cubble)
library(dplyr)

climate_mel |>
  rename(x = long, y = lat) |> 
  face_temporal() |> 
  unfold(x)
#> Error in `validate_index()`:
#> ! Problem while evaluating `index(x)`.
#> Caused by error in `sym()`:
#> ! Can't convert `NULL` to a symbol.
#> ---
#> Backtrace:
#>      â–†
#>  1. ├─base (local) `<fn>`(x)
#>  2. ├─cubble:::print.cubble_df(x)
#>  3. │ ├─base::writeLines(format(x, width = width, ...))
#>  4. │ ├─base::format(x, width = width, ...)
#>  5. │ └─pillar:::format.tbl(x, width = width, ...)
#>  6. │   └─pillar:::format_tbl(...)
#>  7. │     └─pillar::tbl_format_setup(...)
#>  8. │       ├─pillar:::tbl_format_setup_dispatch(...)
#>  9. │       └─pillar:::tbl_format_setup.tbl(...)
#> 10. │         ├─pillar::tbl_sum(x)
#> 11. │         └─cubble:::tbl_sum.temporal_cubble_df(x)
#> 12. │           ├─tsibble::as_tsibble(as_tibble(x), key = key_vars(x), index = index(x))
#> 13. │           └─tsibble:::as_tsibble.tbl_df(...)
#> 14. │             └─tsibble::build_tsibble(...)
#> 15. │               └─tsibble:::validate_index(tbl, !!qindex)
#> 16. │                 └─tidyselect::vars_pull(names, !!index)
#> 17. │                   ├─tidyselect:::with_chained_errors(...)
#> 18. │                   │ └─rlang::try_fetch(...)
#> 19. │                   │   ├─base::tryCatch(...)
#> 20. │                   │   │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 21. │                   │   │   └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 22. │                   │   │     └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 23. │                   │   └─base::withCallingHandlers(...)
#> 24. │                   └─rlang::eval_tidy(expr, set_names(seq_along(vars), vars))
#> 25. └─cubble::index(x)
#> 26.   └─rlang::sym(index_var(data))

When trying for "y" or even when calling the coordinates "X" and "Y" (capital letters) then everythinh works.

climate_mel |>
  rename(x = long, y = lat) |> 
  face_temporal() |> 
  unfold(y)
#> # cubble:   key: id [3], index: date, long form
#> # temporal: 2020-01-01 -- 2020-01-10 [1D], no gaps
#> # spatial:  x [dbl], y [dbl], elev [dbl], name [chr], wmo_id [dbl]
#>    id          date        prcp  tmax  tmin     y
#>    <chr>       <date>     <dbl> <dbl> <dbl> <dbl>
#>  1 ASN00086038 2020-01-01     0  26.8  11   -37.7
#>  2 ASN00086038 2020-01-02     0  26.3  12.2 -37.7
#>  3 ASN00086038 2020-01-03     0  34.5  12.7 -37.7
#>  4 ASN00086038 2020-01-04     0  29.3  18.8 -37.7
#>  5 ASN00086038 2020-01-05    18  16.1  12.5 -37.7
#>  6 ASN00086038 2020-01-06   104  17.5  11.1 -37.7
#>  7 ASN00086038 2020-01-07    14  20.7  12.1 -37.7
#>  8 ASN00086038 2020-01-08     0  26.4  16.4 -37.7
#>  9 ASN00086038 2020-01-09     0  33.1  17.4 -37.7
#> 10 ASN00086038 2020-01-10     0  34    19.6 -37.7
#> # ℹ 20 more rows

climate_mel |>
  rename(X = long, Y = lat) |> 
  face_temporal() |> 
  unfold(X, Y)
#> # cubble:   key: id [3], index: date, long form
#> # temporal: 2020-01-01 -- 2020-01-10 [1D], no gaps
#> # spatial:  X [dbl], Y [dbl], elev [dbl], name [chr], wmo_id [dbl]
#>    id          date        prcp  tmax  tmin     X     Y
#>    <chr>       <date>     <dbl> <dbl> <dbl> <dbl> <dbl>
#>  1 ASN00086038 2020-01-01     0  26.8  11    145. -37.7
#>  2 ASN00086038 2020-01-02     0  26.3  12.2  145. -37.7
#>  3 ASN00086038 2020-01-03     0  34.5  12.7  145. -37.7
#>  4 ASN00086038 2020-01-04     0  29.3  18.8  145. -37.7
#>  5 ASN00086038 2020-01-05    18  16.1  12.5  145. -37.7
#>  6 ASN00086038 2020-01-06   104  17.5  11.1  145. -37.7
#>  7 ASN00086038 2020-01-07    14  20.7  12.1  145. -37.7
#>  8 ASN00086038 2020-01-08     0  26.4  16.4  145. -37.7
#>  9 ASN00086038 2020-01-09     0  33.1  17.4  145. -37.7
#> 10 ASN00086038 2020-01-10     0  34    19.6  145. -37.7
#> # ℹ 20 more rows
huizezhang-sherry commented 7 months ago

Hi Lorena,

Thanks for trying out on cubble!

This is a weird one - somehow index = index(x) on line 12 of the backtrace doesn't like the "x" symbol specifically. I reassign it to "xx" and apparently, it solves the problem :)