I discovered why I was having trouble writing a shapefile out today. 😢
I wanted to post a quick note here, but I also I updated the Rmd/md in the wRangling_Seminar repo: wRangling_Seminar/documents/spatial_data_in_R.Rmd. See line 235, and the ### Save Data as Shapefile, KML, CSV section (lines 267:301).
Basically, it boils down to this, you can't write a shapefile (least I haven't figured out how to) if you read your CSV or data in using the readr::read_csv package and function. You have to use "classic" read.csv, and then everything should work. 👍
The nitty-gritty reason behind this is that when you read things in with read_csv it introduces an attr(*, "spec") list component for each of your columns (click on the environment and see) somewhat behind the scenes...generally I think it's for specifying and indexing the columns when you read in your data (and it might help in indexing and operating on data more rapidly without taking up memory...but that's just a guess). Anyway, when trying to write your shapefile, this causes problems. 🤕
So, simple solution is, just use read.csv!! Also bear in mind the ESRI Shapefiles have a 10 character column title limit, so if you have long variable names, you'll want to trim them down or rename them before you export (or else it happens automagically, which isn't always a good thing).
Hi @gge-ucd/ewoks,
I discovered why I was having trouble writing a shapefile out today. 😢
I wanted to post a quick note here, but I also I updated the Rmd/md in the wRangling_Seminar repo: wRangling_Seminar/documents/spatial_data_in_R.Rmd. See line 235, and the
### Save Data as Shapefile, KML, CSV
section (lines 267:301).Basically, it boils down to this, you can't write a shapefile (least I haven't figured out how to) if you read your CSV or data in using the
readr::read_csv
package and function. You have to use "classic"read.csv
, and then everything should work. 👍The nitty-gritty reason behind this is that when you read things in with
read_csv
it introduces anattr(*, "spec")
list component for each of your columns (click on the environment and see) somewhat behind the scenes...generally I think it's for specifying and indexing the columns when you read in your data (and it might help in indexing and operating on data more rapidly without taking up memory...but that's just a guess). Anyway, when trying to write your shapefile, this causes problems. 🤕So, simple solution is, just use
read.csv
!! Also bear in mind the ESRI Shapefiles have a 10 character column title limit, so if you have long variable names, you'll want to trim them down or rename them before you export (or else it happens automagically, which isn't always a good thing).Check out the code.