Search code examples
rfor-loopdirectoryshapefilelistdir

looping through folders while performing task


I'm trying to loop through folders in a directory while reading and assigning files to variables in R.

The files I want to assign to variables are shapefiles so I use the function readOGR from the rgdal package. The purpose is to later merge all shapefiles belonging to a particular species. The hierarchy/structure of the directory is type1 > species > ids. The shapefiles look like id.shp etc.

Example shapefiles can be downloaded here and here

#code
setwd("~/type1/")

#Extract ids belonging to $species. Later use in readOCR function

sp_id <- function(species){
 wd = "~/type1/"
 list_shp <- list.files(path=paste(wd,species,sep='/'), full.names = F, recursive = F, include.dirs = F)
 vec <- character() 

  for (shp in list_shp){
   y <- unlist(strsplit(shp, '\\.', perl=T))
   vec <- unique(c(vec,y[1]))
  }

 #"1905" "4279"

#Extract dirs for where to perform readOCR function

  list_dir <- list.dirs(path=paste(wd,species,sep='/'), full.names = F, recursive = F)

   for (id in list_dir){
    setwd(id)
    print(getwd())
   }

#"~/type1/speciesX1/1905"
#"~/type1/speciesX1/4279"

    for (i in vec){
     assign(paste("", i, sep=""), readOGR(".", i))
    break
    }
 }

sp_id('speciesX1')

[1] "~/type1/speciesX1/1905"
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "1905"
with 10 features and 3 fields
Feature type: wkbPolygon with 2 dimensions
[1] "~/type1/speciesX1/4279"
Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) : 
Cannot open layer

The problem is that the code only executes readOGR for one shapefile in one of the dirs, seems to change dir again but doesn't execute the last readOGR.


Solution

  • Your function seems to be broken in many different sections, but you could just do:

    library(rgdal)
    
    setwd("~/type1/")
    species <- 'speciesX1'
    list_shp <- list.files(path=species, pattern="*.shp", full.names = TRUE,
                           recursive = TRUE, include.dirs = FALSE)
    shp_objects <- lapply(list_shp, function(x) {readOGR(dsn=x, 
                                                         layer=ogrListLayers(x))})
    

    This will give you a list of SpatialPolygonDataframe objects that you can then merge.