Intermediate R II: Iteration and more functions

Session 10

October 1, 2024

Learning objectives

  • Write a more advanced function
  • Set up an analysis with function and analysis scripts
  • Use iteration to streamline workflows involving functions

When should I write my own function?

  • Standardized procedures done many times
  • Discrete chunks of a workflow
  • Code used by multiple scripts or at multiple points in a workflow

When should I leave code in a script?

  • Single-use, highly-customized code (e.g. a plot)
  • Code that is changing a lot
  • Code that tells the story of your analysis

Questions to ask yourself

  • Do I find myself typing the same code over and over?
  • Am I copy-pasting and changing a single variable/character?
  • Does this code distract from the readability of a script?
  • Is there an existing function I could pull from an R package?

Coding time

Important

Make sure you are in the right project and on an appropriate branch!

Coding time

  • Review sample analysis to decide what could be a function
  • Organize this analysis into analysis and function scripts

Iteration

Iteration

  • Iteration allows you to repeat the same process many times on different objects.
  • for loops:
    • Run sequentially
    • Easy to write and read
    • Slower, can be inefficient
  • purrr::map:
    • Can run in parallel
    • Fairly readable
    • More efficient

Anatomy of for loops

for(<thing> in <some things>) {
  <do something with or to the thing>
}
for(i in 1:10) {
  print(i^2)
}

Storing the output of for loops


squares <- vector(length = 10)

for(i in 1:10){
  squares[i] <- i^2
}

Anatomy of purrr::map

purrr::map(<some things>, <a function>)
purrr::map(1:10, \(x) x^2)

Storing the output of purrr::map

purr_squares <- purrr::map(1:10, \(x) x^2)

Coding time

  • Practice using a for loop and a map statement to apply our function to each island in turn.

Resources

  • R4DS on iteration
  • purrr and furrr documentation

Reproduciblity & Data Science in R

Intermediate R II: Iteration and more functions Session 10 October 1, 2024

  1. Slides

  2. Tools

  3. Close
  • Intermediate R II: Iteration and more functions
  • Learning objectives
  • When should I write my own function?
  • When should I leave code in a script?
  • Questions to ask yourself
  • Coding time
  • Coding time
  • Iteration
  • Iteration
  • Anatomy of for loops
  • Storing the output of for loops
  • Anatomy of purrr::map
  • Storing the output of purrr::map
  • Coding time
  • Resources
  • f Fullscreen
  • s Speaker View
  • o Slide Overview
  • e PDF Export Mode
  • b Toggle Chalkboard
  • c Toggle Notes Canvas
  • d Download Drawings
  • ? Keyboard Help