I'm attempting to adapt a function written by a coworker, but I've hit a point where neither of us know how to solve the issue. Generally, the function reads in several different shapefiles kept in different directories, then merges them all with another necessary .csv dataset (facilitycrosswalk
). I haven't really worked with spatial data in R before, so some of this is totally foreign to me--apologies for anything confusing as this is not my code. Here's a general recreation:
#working directory
working <- "C:/my/main/directory/"
#lists feeding into function
county <- c("County1", "County2", "County3", "County4", "County5")
longsitenames <- c("Site Name 1", "Site Name 2", "Site Name 3")
shortsitenames <- c("SN1", "SN2", "SN3")
#creating function to read and merge all shapefiles
readshapefiles <- function(county){
map2(.x = longsitenames,
.y = shortsitenames,
.f = ~ rgdal::readOGR(
glue("{working}Shared_Data/Spatial Data/Site Data/{county} County/{.x}/Final Shapefile/{.y}.shp" )) %>%
sp::merge(x = .,
y = facilitycrosswalk %>%
mutate(Name = glue("{`Facility Site Name`} Service Area")),
by.x = "Name",
by.y = "Name",
all.x = T,
all.y = F) %>%
st_as_sf(as(., "Spatial")) %>%
filter(method=="censusblock areal interpolation")) %>%
setNames(c(sitenameslong))
}
#running function and formatting output
shp_all <- readshapefiles(county) %>%
reduce(., rbind.sf) %>%
st_as_sf(as(., "Spatial")) %>%
st_transform(st_crs(wgs84))
This gives the error "Error in (function (cond) : error in evaluating the argument 'x' in selecting a method for function 'merge': length(dsn) == 1L is not TRUE"
When I attempt to run the function on one specific county it seems to work okay, which makes me think it's having issues iterating over the list of counties, but I'm not sure why that would be. Any help is greatly appreciated!