HES 505 Fall 2025: Session 6
function
What sorts of things do you often copy and paste?
Has anything gone wrong?
Is it fun to read?
Arguments: What does the function need to know to run?
Parameters: The values you supply to the arguments
Body: The actual steps in the functions recipe
Return: What does the function output?
Use print("some helpful message")
to check intermediate steps in your function
The {{ }}
allows you to pass variables (not values)
use explicit return()
to return more than the last step of the function
What does this do?
What arguments does it accept and what type should those arguments be?
What does it return?
See roxygen2
Pipes are helpful when the output of a function depends on something before it
Prevents intermediate processing steps from flooding your environment
More readable then nested function calls
Thinking about your functions…
|>
and %>%
pass outputs of the left-hand as inputs to the right-hand
Default is first argument of right-hand
.
is the placeholder for %>%
and _
is the placeholder for |>
_
is much simpler and has less functionality
Doing the same thing over and over
for
loops are the classic use
map
and map_
: Do this function for each element of a vector or list
Default returns a list
; suffixes can change this (e.g., map_dfr
)
purrr still does things seriously
furrr allows things to run in parallel
Works when one step isn’t dependent on the other
data
folderscripts
folderdocs
foldergit
Always pull before you start working on anything new
Avoid committing large files
Using .gitignore