On reading in multiple files and applying functions to each simultaneously

I have a project in which I am trying to read in 9 GIS shapefiles into R Studio IDE. I was able to scour the internet and initiate the file.path function to read the shapefiles at once as a list. Past this point is where I begin to have problems.

I'm hoping to get guidance on how to build a comprehensive workflow to perform the same analysis on these shapefiles to efficiently save time, of course. I had trouble even being able to "read" the shapefiles as demonstrated in the reproducible code with different attempts. "St_read" was not effective and neither was "readOGR" along with the "lapply" function, which I believe is the crux of this enterprise.

I'm looking forward to any assistance that can help me efficiently evaluate multiple shapefiles at once.

getwd()
[1] "C:/Users/Akil/Desktop/SpatialDataAnalysis_EHailData/DistanceAnalyses"
#Arranging libraries necessary for loading
BarbaraGordon <- c("rgdal","tidyverse","purrr", "maptools", "ggplot2", "ggmap", "DescTools",

  •                "dplyr", "tidyr", "tmap", "sp", "sf", "jaccard", "datatable",
    
  •                "raster", "GISTools", "ggspatial", "here")
    

#Loading all libraries at once
lapply(BarbaraGordon, library, character.only = TRUE)
Error: package or namespace load failed for ‘ggmap’ in loadNamespace(j <- i[[1L]], c(lib.loc, .libPaths()), versionCheck = vI[[j]]):
there is no package called ‘jpeg’
(bwayne <- file.path("C:/SpatialDataAnalysis_EHailData/DistanceAnalyses", c("JunoDOs_dist_JunoPUs.shp","JunoDO_distLyftPUs.shp",

  •                                                                     "JunoDO_dist_UberPUs.shp","LyftDOs_dist_LyftPUs.shp",
    
  •                                                                     "LyftDOs_dist_JunoPUs.shp",
    
  •                                                                      "LyftDOs_dist_UberPUs.shp", "UberDOs_dist_UberPUs.shp",
    
  •                                                                      "UberDOs_distJunoPUs.shp", "UberDOs_dist_LyftPUs.shp")))
    

[1] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/JunoDOs_dist_JunoPUs.shp"
[2] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/JunoDO_distLyftPUs.shp"
[3] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/JunoDO_dist_UberPUs.shp"
[4] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/LyftDOs_dist_LyftPUs.shp"
[5] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/LyftDOs_dist_JunoPUs.shp"
[6] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/LyftDOs_dist_UberPUs.shp"
[7] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/UberDOs_dist_UberPUs.shp"
[8] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/UberDOs_distJunoPUs.shp"
[9] "C:/SpatialDataAnalysis_EHailData/DistanceAnalyses/UberDOs_dist_LyftPUs.shp"

alfred <- lapply(bwayne, st_read)
Error in match.fun(FUN) : object 'st_read' not found
alfred <- lapply(bwayne, FUN = st_read)
Error in match.fun(FUN) : object 'st_read' not found
list.files(bwayne)
character(0)
listOfShp <- lapply(bwayne, st_read)
Error in match.fun(FUN) : object 'st_read' not found
listOfShp <- lapply(bwayne, readOGR)
Error in ogrListLayers(dsn = dsn) : Cannot open data source


Thank you

lapply is good but I think using a loop is easier for some things.

mylist <- ... # my list of objects
for (i in seq_along(mylist)){
   # do stuff to mylist[[i]]
}

You can also start off with an empty list and build the list inside the loop.

mylist <- vector("list", number_of_items) # empty list of objects 
for (i in seq_along(mylist)){
   mylist[[i]] <- ... # read in or create mylist[[i]]
   # do stuff to mylist[[i]]
}

That's an awkward method of loading libraries.

When I installed jaccard I couldn't find a dependency called qvalue. I had to get it from here:

Did you mean "data.table" with a dot? I can't find "datatable"

st_read is not found because the sf package has not been successfully loaded.

OK, thank you for your response. As for the library loading, I'm still very new to programming, in RStudio and in general. I learned this method from an online data science source that I can't name now, but I always thought it was very effective. It appears with this troubleshooting that you have alerted me to an inefficiency. I will edit accordingly.

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