library(stars)
## Loading required package: abind
## Loading required package: sf
## Linking to GEOS 3.8.1, GDAL 3.1.3, PROJ 7.1.1
library(starsExtra)

# Raster
filename = system.file("tif/L7_ETMs.tif", package = "stars")
r = read_stars(filename)
plot(r)

# Polygons
set.seed(1)
pnt = st_sample(st_as_sfc(st_bbox(r)), 5)
pol = st_buffer(pnt, 1000)

# Plot
plot(r[,,,1], reset = FALSE)
plot(pol, border = "yellow", add = TRUE)
text(st_coordinates(st_centroid(pol)), as.character(1:length(pol)), col = "yellow")

# Extract
extract2(r, pol, mean, na.rm = TRUE, progress = FALSE)
##          [,1]     [,2]     [,3]     [,4]     [,5]     [,6]
## [1,] 69.14238 57.36526 53.45094 73.01760 90.12011 58.45819
## [2,] 67.18994 54.89603 49.23681 74.19708 84.03104 51.53693
## [3,] 73.58986 63.26274 61.30566 77.40186 94.32170 61.79079
## [4,] 92.93088 83.16531 76.60105 38.13325 56.43995 44.96951
## [5,] 79.36380 63.92055 64.25269 53.32527 87.32019 67.66368