This works for me:
temp <- structure(savelabel = c(
"3063731380325",
"3073749905003"), data = list(
structure(list(x = c(
"x", "306210.926", "306317.914", "306358.873",
"306366.352", "306368.777", "306260.655", "306259.157", "306256.893",
"306209.826", "306210.926"
), y = c(
"y", "4373977.512", "4373838.333",
"4373869.788", "4373874.506", "4373876.006", "4374015.196",
"4374014.559", "4374015.263", "4373978.943", "4373977.512"
)), class = "data.frame", row.names = c(NA, -11L)), structure(list(
x = c(
"x", "307861.71", "307860.036", "307856.292", "307898.422",
"307949.512", "307956.067", "307861.73", "307861.71"
),
y = c(
"y", "4374154.479", "4374095.852", "4373964.716",
"4373964.716", "4373963.374", "4374151.803", "4374155.143",
"4374154.479"
)
), class = "data.frame", row.names = c(
NA,
-9L
))
)), row.names = c(NA, -2L), class = c(
"tbl_df", "tbl",
"data.frame"
))
# Your function
library(dplyr)
library(purrr)
library(tibble)
library(sp)
library(rgdal)
myfunction <- function(temp, save_name) {
# remove the 1st row of the .txt file
d2 <- temp[-1, ]
d3 <- substr(d2$x, 1, 6)
d4 <- substr(d2$y, 1, 7)
df <- cbind(d3, d4)
df1 <- as.data.frame(df)
b <- apply(as.matrix(df1), 2, as.character)
# rename the column names of the matrix
colnames(b) [1] <- paste("x")
mode(b) <- "numeric"
b2 <- as.data.frame(b)
ch <- chull(b2)
coords <- b2[c(ch, ch[1]), ]
sp_poly <- SpatialPolygons(list(Polygons(list(Polygon(coords)), ID = 1)))
proj4string(sp_poly) <- CRS("+init=epsg:2100")
sp_poly_df <- SpatialPolygonsDataFrame(sp_poly, data = data.frame(ID = 1))
test1 <- spTransform(sp_poly_df, "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
writeOGR(
obj = test1, dsn = "mydir", layer = "r",
driver = "ESRI Shapefile", overwrite_layer = TRUE
)
file.rename(from = "mydir/r.shp",paste0("mydir/",save_name,".shp"))
file.rename(from = "mydir/r.shx",paste0("mydir/",save_name,".shx"))
file.rename(from = "mydir/r.dbf",paste0("mydir/",save_name,".dbf"))
file.rename(from = "mydir/r.prj",paste0("mydir/",save_name,".prj"))
}
walk2(temp$data, temp$savelabel, ~ myfunction(.x, .y))