why error in gnp[[1]] : subscript out of bounds

library(sf)
library(rgdal)
library(terra)
library(tmap)
library(dplyr)
library(magrittr)
library(purrr)
library(rnaturalearth)
library(rnaturalearthdata)
library(ggplot2)
library(leaflet)
library(mapview)
library(grid)
library(ggmap)
library(basemaps)
library(raster)
library(rnaturalearthhires)

install.packages("basemaps")

setwd("E:/GIS_DATA/Glacier_Alaska_and_Western")

ak <- st_read("01_rgi60_Alaska.shp")
wes <- st_read("02_rgi60_WesternCanadaUS.shp")

ak
wes

str(ak)
str(wes)

st_crs(ak) == st_crs(wes)

st_bbox(ak) == st_bbox(wes)

tmap_options(check.and.fix = TRUE)

nwa_bbox <- st_bbox(
st_union(
st_as_sfc(st_bbox(wes)),
st_as_sfc(st_bbox(ak))
)
)

tm_shape(ak, bbox = nwa_bbox) +
tm_polygons()+
tm_shape(wes)+
tm_polygons()+
tm_layout(
title = "Glaciers of Western North America",
title.position = c("center", "top"),
title.size = 1.1,
bg.color = "#fcfcfc",
inner.margins = c(0.06, 0.01, 0.09, 0.01),
outer.margins = 0,
frame.lwd = 0.2
)+
tm_compass(
type = "arrow",
position = c("right", "top"),
size = 1.2,
text.size = 0.6
)+
tm_scale_bar(
breaks = c(0, 1000, 2000),
position = c("right", "BOTTOM")
)

states_all <- ne_states(
country = c("canada", "united states of america"),
returnclass = "sf"
)

states <- states_all %>%
filter(name_en == "Alaska" |
name_en == "British Columbia" |
name_en == "Yukon" |
name_en == "Northwest Territories" |
name_en == "Alberta" |
name_en == "California" |
name_en == "Washington" |
name_en == "Oregon" |
name_en == "Idaho" |
name_en == "Montana" |
name_en == "Wyoming" |
name_en == "Colorado"
)

st_crs(states) == st_crs(ak)

st_bbox(states) == nwa_bbox

tm_shape(states, bbox = nwa_bbox)+
tm_polygons(col = "#f2f2f2",
lwd = 0.2)+
tm_shape(ak)+
tm_borders(col = "#3399ff")+
tm_fill(col = "#86baff")+
tm_shape(wes)+
tm_borders(col = "#3399ff")+
tm_borders(col = "#86baff")+
tm_layout(
title = "Glaciers of Western North America",
title.position = c("center", "top"),
title.size = 1.1,
bg.color = "#fcfcfc",
inner.margins = c(0.06, 0.01, 0.09, 0.01),
outer.margins = 0,
frame.lwd = 0.2
)+
tm_compass(
type = "arrow",
position = c("right", "top"),
size = 1.2,
text.size = 0.6
)+
tm_scale_bar(
breaks = c(0, 1000, 2000),
position = c("right", "BOTTOM")
)

Load required libraries

library(sf)
library(dplyr)
library(purrr)

create a function that reads and cleans the data

prep <- function(dir) {
g <- st_read(dir)
g %<>% rename_with(~ tolower(gsub("Area....", "area", .x)))
g %<>% select(
year,
objectid,
glacname,
area,
shape_leng,
x_coord,
y_coord,
source_sca,
source
)
}

create a vector of dataset names

dirs <- grep("GNPglaciers_.*", list.dirs(), value = T)

pass each element of that vector through prep() thanks to map()

gnp <- map(dirs, prep)

st_crs(gnp[[1]]) == st_crs(gnp[[2]])
st_crs(gnp[[1]]) == st_crs(gnp[[3]])
st_crs(gnp[[1]]) == st_crs(gnp[[4]])

Here's what's happening

library(purrr)
setwd("/Users/ro/projects/")
dirs <- list.dirs() |> tail()
dirs
#> [1] "./viml-to-lua/.git/refs"               
#> [2] "./viml-to-lua/.git/refs/heads"         
#> [3] "./viml-to-lua/.git/refs/remotes"       
#> [4] "./viml-to-lua/.git/refs/remotes/origin"
#> [5] "./viml-to-lua/.git/refs/tags"          
#> [6] "./viml-to-lua/lua"
gnp <- map(dirs,dir) |> tail()
gnp
#> [[1]]
#> [1] "heads"   "remotes" "tags"   
#> 
#> [[2]]
#> [1] "main"
#> 
#> [[3]]
#> [1] "origin"
#> 
#> [[4]]
#> [1] "HEAD"
#> 
#> [[5]]
#> character(0)
#> 
#> [[6]]
#> [1] "autocompletion.lua"   "lsp-config.lua"       "mappings.lua"        
#> [4] "plugins.lua"          "run-current.lua"      "settings.lua"        
#> [7] "telescope-config.lua" "test-vim.lua"         "vue.lua"
str(gnp[1])
#> List of 1
#>  $ : chr [1:3] "heads" "remotes" "tags"
length(gnp)
#> [1] 6
gnp[[1]] == gnp[[6]]
#> [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
gnp[[1]] == gnp[[7]]
#> Error in gnp[[7]]: subscript out of bounds

Created on 2023-09-02 with reprex v2.0.2

Your gnp isn't long enough to handle all of the subsets.