2

I would like to ask another question, which includes SpatialPolygons. In order to make it reproducible I wanted to use dput() for the SpatialPolygons object, but its not outputting a reproducible structure.

Why can I use dput() with SpatialPoints, but not with Lines/SpatialLines, Polygons/SpatialPolygons?

Is the only workaround, to export the coordinates and recreate the SpatialPolygons in the example?

Test Data:

library(sp)
df = data.frame(lon=runif(10, 15,19), lat=runif(10,40,45))

dput(SpatialPoints(coordinates(df)))

dput(Lines(list(Line(coordinates(df))), 1))
dput(SpatialLines(list(Lines(list(Line(coordinates(df))), 1))))

dput(Polygons(list(Polygon(df)), 1))
dput(SpatialPolygons(list(Polygons(list(Polygon(df)), 1))))

dput(SpatialPolygons(list(Polygons(list(Polygon(df)), 1))), control="all")

The dupt2() method from this answer works for Lines/SpatialLines but not for Polygons/SpatialPolygons, where this error occurs:

Error in validityMethod(object) : object 'Polygons_validate_c' not found

  • So how to make a SpatialPolygons-object reproducible?

A workaround would be to convert the objects to simple features and then use dput(). They can obviously be deparsed.

Example using LINESTRING and POLYGON:

library(sp)
library(sf)
df = data.frame(lon=runif(10, 15,19), lat=runif(10,40,45))

SLi = SpatialLines(list(Lines(list(Line(coordinates(df))), 1)))
SPo = SpatialPolygons(list(Polygons(list(Polygon(df)), 1)))

dput(st_as_sf(SLi))
dput(st_as_sf(SPo))
SeGa
  • 9,454
  • 3
  • 31
  • 70
  • please add library calls to your function, seems like you're using package sp – moodymudskipper Jul 25 '18 at 15:13
  • `dput(SpacialLines)` works for me, however, with polygons I get the error `Error in validityMethod(object) : object 'Polygons_validate_c' not found` – Mako212 Jul 25 '18 at 15:15
  • Library added. Yes the dput() method works, but the output wont be reproducible. It will incldue something like `lines = list()` – SeGa Jul 25 '18 at 15:17
  • 1
    from `?dput`, `Deparsing an object is difficult, and not always possible. With the default control, dput() attempts to deparse in a way that is readable, but for more complex or unusual objects (see dump), not likely to be parsed as identical to the original.` – moodymudskipper Jul 25 '18 at 15:18
  • and `This is not a good way to transfer objects between R sessions. dump is better, but the function save is designed to be used for transporting R data, and will work with R objects that dput does not handle correctly as well as being much faster. ` So maybe use `dump` instead ? – moodymudskipper Jul 25 '18 at 15:19
  • I read the manuals of dput. Using `dump` doesnt work at all. It will give this `error: Error in as.list.default(X) : no method for coercing this S4 class to a vector`. I just want to know how to make these objects reproducible. And if the only way is to `dput` the coordinates and recreate the Spatial-Objects, thats fine for me. Just looking for some experienced insights. :) – SeGa Jul 25 '18 at 15:23
  • @SeGa have you find a way ? – Myr TH Nov 24 '22 at 12:42

1 Answers1

1

After running the code I mentioned in the comments, I decided I would offer a tentative solution and see if you a) have the same results on your system, and b) whether it addressed the issues you were having.

 newSpPa <- dput(SpatialPolygons(list(Polygons(list(Polygon(df)), 1))), control="all")
 oldSpPa <- SpatialPolygons(list(Polygons(list(Polygon(df)), 1))) 
 identical(oldSpPa, newSpPa) 
#[1] TRUE

It wasn't clear from my reading your question whether the return of a call to new("SpatialPolygons", ...) was deemed to be unsatisfactory. I think the assignment step that I did was different than your code and it's possible that my assignment would only succeed in the setting of previously defined objects being in the workspace at the time of creation. If that's the case then I think the typical suggestion would be to do this in the setting of package-creation.

SeGa
  • 9,454
  • 3
  • 31
  • 70
IRTFM
  • 258,963
  • 21
  • 364
  • 487
  • No I'm not getting the same results. I'm getting a warning message for the first line in your code: `In dput(methods::slot(x, n), file = file, control = control) : deparse of an S4 object will not be source()able`. and `newSpPa` will be NULL and therefore the objects wont be identical. – SeGa Jul 27 '18 at 10:42