Pipes, Functions, and Iteration

Content for Wednesday, September 10, 2025

In our last section of “The Fundamentals”, we’ll take on one of the trickier (at least for me) parts of programming in R: writing functions and iteration. Many of the tasks we’ll complete in processing and analyzing spatial data require repeating the same operation(s) on multiple files. Not only that, but some of the “weirdness” of “found” spatial data means that we might need more flexibility in functions than the default version of tidyverse functions. For that reason, we’re going to try and build a little intuition for building our own functions and using the iteration capabilities of purrr and dplyr on tabular data before we get started on spatial data.

Setting the Stage

  • Scripts, algorithms, and functions - chapter 11 in in Lovelace et al., Geocomputation with R (Lovelace et al. 2019) introduces some concepts behind geospatial programming. A few of these pieces will make more sense in the next few weeks, but the general advice on constructing code and planning analyses is useful now.

Technical Details

  • Functions - from R for Data Science by Wickham (2016) provides an overview of writing functions and ideas for when functions are necessary.

  • Writing Functions - From Vanderplas (vanderplas2025?) Statistical Computing using R and python takes things a little further by describing function environments and scopes. There’s also nice examples of translating pseudocode into R functions.

  • Debugging - from the same book has great advice for working through errors, building reproducible examples, and coding defensively to avoid errors in the first place.

  • Iteration - from R for Data Science by Wickham (2016) introduces the purrr package and the map_ family of functions.

  • Learning to purrr provides a nice justification for why we might prefer the map_ family over the apply family from base R along with more explanation of many of the examples from Wickham.

purrr <-> base R from the purrr manual provides a nice crosswalk between functions in the apply family and functions in the purrr family.

Objectives

By the end of today you should be able to:

  • Use pseudocode to write out the “algorithm” for a function.

  • Write your own simple functions to automate repetitive tasks.

  • Practice using iteration to avoid copy-and-paste.

Slides

The slides for today’s lesson are available online as an HTML file. Use the buttons below to open the slides either as an interactive website or as a static PDF (for printing or storing for later). You can also click in the slides below and navigate through them with your left and right arrow keys.

View all slides in new window Download PDF of all slides

References

Lovelace, R., J. Nowosad, and J. Muenchow. 2019. Geocomputation with R. CRC Press.
Wickham, H., and G. Grolemund. 2016. R for data science: Import, tidy, transform, visualize, and model data. " O’Reilly Media, Inc.".