| mask_stars | R Documentation | 
You can use st_crop(...) but it runs slowly for large rasters
mask_stars(x, y, invert = FALSE, filename = NA)
x | 
 stars object  | 
y | 
 sf polygon object. Pixels exterior of this as assigned NA  | 
invert | 
 logical, if TRUE values inside the polygon are set to NA  | 
filename | 
 character or NA, if a filename write to the file  | 
a masked stars object, areas outside (or possibly inside) are set to NA
depending upon the value of invert.  Default is outside is NA
https://github.com/r-spatial/stars/issues/622
## Not run: 
  library(stars)
  library(sf)
  x = read_stars(system.file("tif/L7_ETMs.tif", package = "stars")) 
  m = cbind(c(290000, 295000, 292500, 290000),
            c(9113000, 9115000, 9118000, 9113000))
  p = st_sfc( st_polygon(list(m)) ) |> 
    st_set_crs(31985)
  m = mask_stars(x, p)
  plot_p = function() plot(p, add = TRUE, border = 'orange')
  z = c(x,m, along = 'band')
  plot(z, hook = plot_p)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.