This is a very short note about how to save Simple Features or sp
file into a Rds
file. When downloading a map data, it contains multiple file in a folder such as shp
, dbf
, prj
, shx
etc. This is quite messy after you have some manipulations.
By saving into a single rds
, it is easy to read and store. I use Montreal map as example. All we need is only the sf
and dplyr
for manipulations and more importantly, readr
from tidyverse to write and read.
library(sf)
library(dplyr)
mtl <- read_sf("maps/mtl.shp")
# some random manipulation
mtl <- mtl %>% select(NOM, TYPE)
# save as RDS, path can be specified, sometime you may compress to save some space
readr::write_rds(mtl, path = file.path("maps/mtl.rds"), compress = 'gz')
# read RDS file
montreal <- readr::read_rds('maps/mtl.rds')
# you may verify that they are the same
identical(mtl, montreal)