tidyverse / magrittr

Improve the readability of R code with the pipe
https://magrittr.tidyverse.org
Other
957 stars 157 forks source link

Pipe for spatial objects #213

Closed aouazad closed 4 years ago

aouazad commented 4 years ago

A simple suggestion and please feel free to shoot it down if needed.

I very often work with SpatialPolygonsDataFrame objects, which have a data slot.

I often type

sf <- readOGR("my_map.shp") %>%
        mutate(a_new_var = f(old_var))

only to realize that the pipe operator works with data frames.

It would be convenient to have a pipe operator for Spatial objects, which I think are used quite frequently, such as:

sf <- readOGR("my_map.shp") %m>%
        mutate(a_new_var = f(old_var))

which modifies the data slot only yet returns the entire SpatialPolygonsDataFrame.

I am writing a small code snippet in my repo to have this, and wondering whether it'd benefit the whole community. It definitely shortens my code.

This could also be best placed in a spatial package than the general purpose magrittr.

Best

lionel- commented 4 years ago

The pipe operator works with any kind of inputs, not just data frames. Probably you'd want sp to implement dplyr methods for their data structures.

aouazad commented 4 years ago

Actually this is solved by the sf package, that implements piping for spatial objects.