I have a nested list with multiple shapefiles and I want to use lapply with writeOGR ('rgdal') in order to export all the shapefiles. Sorry for not giving a reproducible example.
# My list
shp.list
$e_ang
$e_ang$e_ang_g
class : SpatialPolygonsDataFrame
features : 1291
extent : -1265736, 869263.7, -3051349, -1022949 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=eqdc +lat_0=0 +lon_0=25 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
variables : 1
names : e_ang_g
min values : 1
max values : 1
$e_ang$e_ang_l
class : SpatialPolygonsDataFrame
features : 1291
extent : -1265736, 869263.7, -3051349, -1022949 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=eqdc +lat_0=0 +lon_0=25 +lat_1=20 +lat_2=-23 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
variables : 1
names : Value
min values : 1
max values : 1
Here is the code I've used
# Using lapply to write shapefiles within list
lapply(unlist(shp.list), function(x) writeOGR(object = x,
dsn = paste(fruit.bats, "output", sep = ''), # folder to store shapefiles
layer = names(x), # shapefile names as in shp.list
driver = 'ESRI Shapefile',
layer_options = 'RESIZE=YES',
overwrite_layer = T))
# this returns
$e_ang.e_ang_g
NULL
$e_ang.e_ang_l
NULL
I believe the problem is in the argument 'layer' in writeOGR. Probably I can create a vector to pass the names to the function, but I was wondering if there is a more direct way to do it. I would like the shapefiles to have the name of the nested elements, 'e_ang_g' and 'e_ang_l' in the example.
I was able to do the same with a loop, but still seek a solution using lapply.
Here is the code for the loop.
n <- substring(names(unlist(shp.list)), 7,13) # vector with shapefile names
for (j in 1:length(n)){
writeOGR(unlist(shp.list)[[j]],
dsn = paste(fruit.bats, "output", sep = '/'),
layer = n[j],
driver = 'ESRI Shapefile',
layer_options = 'RESIZE=YES',
overwrite_layer = T) }
pts=data.frame(x=1:4,y=1:4);coordinates(pts)=~x+y;shp.list = list(e_ang = list(e_ang_g=pts,e_ang_l=pts), z_foo=list(a_foo=pts,b_foo=pts))