library(raster)
## Loading required package: sp
r <- raster(volcano)
## this is SpatialPolygonsDataFrame just for example, but mask works with sf too
p <- rasterToPolygons(r %/% 25, dissolve = T)
## Loading required namespace: rgeos
library(sf)
## Linking to GEOS 3.6.1, GDAL 2.2.3, proj.4 4.9.3
## pretend we read p in with st_read/read_sf
p <- st_as_sf(p[3:nrow(p), ])
plot(mask(r, p))
plot(st_union(st_geometry(p)), add = TRUE)