r-spatial / stars

Spatiotemporal Arrays, Raster and Vector Data Cubes
https://r-spatial.github.io/stars/
Apache License 2.0
563 stars 94 forks source link

`mutate.stars` fails on attributes with spaces in its name #689

Closed pepijn-devries closed 5 months ago

pepijn-devries commented 5 months ago

Thanks for your excellent work on the stars package. I came across the following issue:

When an attribute's name contains spaces, mutate.stars cannot be applied to this attributes (see reprex below). It appears that the function repairs the name in the process and replaces the spaces with periods (dots), after which the original name is no longer found. In dplyr mutate does not (attempt to) repair names as far as I'm aware. Would it make sense to skip the name repair, or make it optional in mutate.stars?

Cheers,

Pepijn

library(stars)
#> Warning: package 'stars' was built under R version 4.3.3
#> Loading required package: abind
#> Loading required package: sf
#> Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
matrix(0, 10, 10) |>
  st_as_stars() |>
  rename(`name with spaces` = 1) |>
  mutate(`name with spaces` = `name with spaces` + 1)
#> Error in `dplyr::mutate()`:
#> ℹ In argument: `name with spaces = `name with spaces` + 1`.
#> Caused by error:
#> ! object 'name with spaces' not found
#> Backtrace:
#>      ▆
#>   1. ├─dplyr::mutate(...)
#>   2. ├─stars:::mutate.stars(...)
#>   3. │ ├─dplyr::mutate(to_df(.data), ...)
#>   4. │ └─dplyr:::mutate.data.frame(to_df(.data), ...)
#>   5. │   └─dplyr:::mutate_cols(.data, dplyr_quosures(...), by)
#>   6. │     ├─base::withCallingHandlers(...)
#>   7. │     └─dplyr:::mutate_col(dots[[i]], data, mask, new_columns)
#>   8. │       └─mask$eval_all_mutate(quo)
#>   9. │         └─dplyr (local) eval()
#>  10. └─base::.handleSimpleError(...)
#>  11.   └─dplyr (local) h(simpleError(msg, call))
#>  12.     └─rlang::abort(message, class = error_class, parent = parent, call = error_call)

Created on 2024-06-03 with reprex v2.0.2

Note that this does work:

matrix(0, 10, 10) |>
  st_as_stars() |>
  rename(`name with spaces` = 1) |>
  mutate(name.with.spaces = name.with.spaces + 1)
edzer commented 5 months ago

Thanks!