0

I am trying to load the state map from the maps package into an R object. I am hoping it is a SpatialPolygonsDataFrame or something I can turn into one after I have inspected it. However I am failing at the first step – getting it into an R object. I do not know the file type.

I first tried to assign the map() output to an R object directly:

st_m <- maps::map(database = "state")

draws the map, but str(st_m) appears to do nothing, unless it is redrawing the same map.

Then I tried loading it as a dataset: st_m <- data("stateMapEnv", package="maps") but this just returns a string:

> str(stateMapEnv)
 chr "R_MAP_DATA_DIR"

I opened the maps directory win-library/3.4/maps/mapdata/ and found what I think is the map file, “state.L”.

I tried reading it with scan and got an error message I do not understand:

scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in scan(file = "D:/Documents/R/win-library/3.4/maps/mapdata/state.L") : 
  scan() expected 'a real', got '#'

I then opened the file with Notepad++. It appears to be a binary or compressed file.

So I thought it might be an R data file with an unusual extension. But my attempt to load it returned a “bad magic number” error:

st_m <- load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L")
Error in load("D:/Documents/R/win-library/3.4/maps/mapdata/state.L") : 
      bad restore file magic number (file may be corrupted) -- no data loaded

Observing that these responses have progressed from the unhelpful through the incomprehensible to the occult, I thought it best to seek assistance from the wizards of stackoverflow.

Jaap
  • 81,064
  • 34
  • 182
  • 193
andrewH
  • 2,281
  • 2
  • 22
  • 32
  • Don't understand what you mean by `str(st_m)` shows nothing. It seems to be just a list of x, y coordinates and state names – OganM May 01 '18 at 23:38
  • andrewH, what do you expect the `str` function does? I expect it to dump out a summary of the `str`ucture of an object, but not execute anything on its presentation. In contrast, some classes override their `print.classtype` function to do something special; for instance `p <- ggplot(...)` is quite fast, and does only assignment; the `print.ggplot` function is what actually *plots* it, which is its "printing side-effect", but `str(p)` does nothing like that, it just barfs out structure. (However, it would be possible to write `ggplot2::str.ggplot` and have some fun ...) – r2evans May 01 '18 at 23:44
  • Look at the docs (`?maps::map`) to see that `maps::map("state")` returns an object of type `map` and optionally plots the shape. You'd want to then convert that object to a `Spatial*` object – camille May 02 '18 at 00:26
  • 1
    Possible duplicate of [Converting a "map" object to a "SpatialPolygon" object](https://stackoverflow.com/questions/26062280/converting-a-map-object-to-a-spatialpolygon-object) – camille May 02 '18 at 00:26

1 Answers1

0

This should be able to export the 'state' or any other maps dataset for you:

library(ggplot2)
state_dataset <- map_data("state")
Farshad
  • 51
  • 3