How to convert RDS file (list with four elements, all lists) to an ESRI shapefile

I'm a complete beginning with R - :S

I received a .rds file which I know contains 4 polygons - I would like to convert them to ESRI shapefiles. I searched for how to do this and tried the following:


x <- readRDS("path/to/the/rds_file.rds")

rgdal::writeOGR(x, "path/to/destination", "filename", driver = "ESRI Shapefile")

But I always get the following error:
Error: inherits(obj, "Spatial") is not TRUE

The outputs for classof(x) and type(x) are both lists.

Can anyone help me?

Is it a multipolgon file?

You could just use st_write() in the the sf package if it is.

e.g. st_write(object, "filename.shp")

Otherwise, you could write them separately, or merge them if you want them in one object, then write.

Thank you for responding so quickly!

I tried this but I get the following error message:

no applicable method for 'st_write' applied to an object of class "list"

So is your object a list of four polygons?

1 Like

yes it is - It's a list with four elements, all also lists, containing a polygon each... (from what I can tell)

I suppose you could bind rows (using dplyr) or rbind (in base). Then export. It would be one polygon.

x <- readRDS("path/to/the/rds_file.rds") %>% 
1 Like

This command seems to have worked. I followed with st_write(object, "filename.shp") which also seems to have worked!

But now I don't know where my shapefile got saved :smiley: - it's not in my working directory... any idea? Sorry for these really basic questions!!!

Try getwd() and see the output of that.

Best to use the projects workflow though. Have a read.

1 Like

Thank you so much for your help! It seems to have worked but the only problem is that there was some data loss, so I tried extracting each element from the list, and did the same thing one by one to create four shapefiles - and so far so good! Couldn't have done it without you!

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.