library(sf)
library(mapview)
library(mapedit)


# load North Carolina map
nc <- st_read(system.file("shape/nc.shp", package="sf"))

# generate random points
points <- st_sample(nc, 200) |> st_sf() |> st_transform(4326)

# plot points and NC border
mapview(nc, col.region = "gray80") + mapview(points)

# plot points and open interactive map to draw polygon, then click "Done"
my_polygon <- mapview(points) |> editMap()

# extract the polygon we drew
poly <- my_polygon$finished

# keep points inside the polygon
new_points <- st_intersection(points, poly)

# plot polygon and selected points
mapview(nc, col.region = "gray80") + mapview(poly) + mapview(new_points)

