You will need a combination of raster::mask() and raster::crop() calls; consider the example linked here:
raster::mask()
raster::crop()