Following on from my recent experience with deleting files using R, I found myself needing to copy a large number of raster files from a folder on my computer to a USB drive so that I could post them to a colleague (yes, snail mail – how old and antiquated!). While this is not typically a difficult task to do manually, I didn’t want to copy all of the files within the folder and there was no way to sort the folder in a sensible manner that meant I could select out the files that I wanted without individually clicking on all 723 files (out of ~4,300) and copying them over. Not only would this have been incredibly tedious(!), it’s highly likely that I would have made a mistake and missed something important or copied over files that they didn’t need. So enter my foray into copying files using R. Continue reading
Tag Archives: tutorial
Converting shapefiles to rasters in R
I’ve been doing a lot of analyses recently that need rasters representing features in the landscape. In most cases, these data have been supplied as shapefiles, so I needed to quickly extract parts of a shapefile dataset and convert them to a raster in a standardised format. Preferably with as little repetitive coding as possible. So I created a simple and relatively flexible function to do the job for me.
The function requires two main input files: the shapefile (shp
) that you want to convert and a raster that represents the background area (mask.raster
), with your desired extent and resolution. The value of the background raster should be set to a constant value that will represent the absence of the data in the shapefile (I typically use zero).
The function steps through the following:
- Optional: If
shp
is not in the same projection as themask.raster
, set the current projection (proj.from
) and then transform the shapefile to the new projection (proj.to
) usingtransform=TRUE
. - Convert
shp
to a raster based on the specifications ofmask.raster
(i.e. same extent and resolution). - Set the value of the cells of the raster that represent the polygon to the desired
value
. - Merge the raster with
mask.raster
, so that the background values are equal to the value ofmask.raster
. - Export as a tiff file in the working directory with the
label
specified in the function call. - If desired, plot the new raster using
map=TRUE
. - Return as an object in the global R environment.
The function is relatively quick, although is somewhat dependant on how complicated your shapefile is. The more individual polygons that need to filtered through and extracted, the longer it will take. Continue reading
Remotely deleting files from R
Sometimes programs generate a LOT of files while running scripts. Usually these are important (why else would you be running the script?). However, sometimes scripts generate mountains of temporary files to create summary outputs that aren’t really useful in their own right. Manually deleting such temporary files can be a very time consuming and tedious process, particularly if they are mixed in with the important ones. Not to mention the risk of accidentally deleting things you need because you’ve got bored and your mind has wandered off to more exciting things…

…like watching orca swim past the hut window!
I had exactly this problem a few months ago when I had ~65,000 temp files from a modelling process that were no longer needed, inconveniently mixed in with the things I needed to keep. Clearly deleting these files manually wasn’t really going to be an option. There are a number of ways to tackle this problem but R provided a simple two-line solution to the problem. Continue reading