library("sf")
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library("sp")
setwd("data/Shelby.gdb")

#reading geodatabase

st_read(dsn = "data/Shelby.gdb")
## Multiple layers are present in data source C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb, reading layer `Community_Garden_shelby'.
## Use `st_layers' to list all layer names and their type in a data source.
## Set the `layer' argument in `st_read' to read a particular layer.
## Warning in evalq((function (..., call. = TRUE, immediate. = FALSE, noBreaks. =
## FALSE, : automatically selected the first layer in a data source containing more
## than one.
## Reading layer `Community_Garden_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 33 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 756844.4 ymin: 298726.4 xmax: 794158.8 ymax: 341723.2
## projected CRS:  NAD83 / Tennessee (ftUS)

#output only shows the first layer

#creating a new object to take on all layers

layers <- st_layers(dsn = "data/Shelby.gdb")

#saving layers using a for loop

for(i in layers$name){st_read("data/Shelby.gdb", layer = i)}
## Reading layer `Community_Garden_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 33 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 756844.4 ymin: 298726.4 xmax: 794158.8 ymax: 341723.2
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `School_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 391 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 734398 ymin: 263306.1 xmax: 876914 ymax: 397840.4
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Rail_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 339 features and 8 fields
## geometry type:  MULTILINESTRING
## dimension:      XY
## bbox:           xmin: 727063.6 ymin: 263436.6 xmax: 883773.4 ymax: 407636.1
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Parks_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 226 features and 6 fields
## geometry type:  GEOMETRY
## dimension:      XY
## bbox:           xmin: 732044.5 ymin: 265123.9 xmax: 878833.9 ymax: 410431.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Library_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 22 features and 16 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 752812.9 ymin: 272549.6 xmax: 875503.7 ymax: 390129.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Law_Enforcement_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 37 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 752117.2 ymin: 278903.1 xmax: 872274.7 ymax: 390481
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Hospital_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 21 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 760308.5 ymin: 279451.1 xmax: 856890.3 ymax: 351008.6
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Health_Centers_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 31 features and 16 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 757687.9 ymin: 277327.6 xmax: 872210.3 ymax: 391961
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Grocer_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 60 features and 16 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 755399 ymin: 271441.9 xmax: 875874.3 ymax: 401002.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Fire_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 89 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 739899.8 ymin: 269570.5 xmax: 873174.7 ymax: 392902.6
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `Farmer_Market_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 14 features and 17 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 754547.3 ymin: 279243.4 xmax: 875874.3 ymax: 390219.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `County_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 1 feature and 23 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 677966.9 ymin: 261279.3 xmax: 885736.8 ymax: 412664.8
## projected CRS:  NAD83 / Tennessee (ftUS)
## Reading layer `roads_shelby' from data source `C:\Users\asharp5\Desktop\r-intro\Unit 2. Using Spatial Data with R\R-spatial\st_read\data\Shelby.gdb' using driver `OpenFileGDB'
## Simple feature collection with 2763 features and 8 fields
## geometry type:  MULTILINESTRING
## dimension:      XY
## bbox:           xmin: -90.12757 ymin: 34.99427 xmax: -89.6358 ymax: 35.40568
## geographic CRS: WGS 84

#the loop successfully reads all 13 layers at once