Rasters and Shapefiles

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