tidyverse / magrittr

Improve the readability of R code with the pipe

Home Page:https://magrittr.tidyverse.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pipe for spatial objects

aouazad opened this issue · comments

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

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.

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