This post shows a simple example of how to work with rasters and vectors using the terra package. Terra replaces the older raster package, since terra is usually faster to use.
Then create some objects to work with and plot them.
Data.
Vector operations
Buffer
Buffer.
Intersection
Intersection.
Raster operations
Mask values
Masked raster.
Operations over all cells
Sum of both bands.
Global operations
Focal operations
Focal max.
Raster vector operations
Crop
Cropped images.
Mask
Masked image (seems nothing happened due to overlap between raster and polygon).