write a spatial dataframe to a shapefile:

shapewriter=function(data, name){
  require(rgdal)
  rgdal::writeOGR(data, dsn = dirname(name), 
    layer = tools::file_path_sans_ext(basename(name)), 
    driver = 'ESRI Shapefile', verbose = FALSE, overwrite_layer = TRUE)
}