6.17 withr pre-made helpers

  • local_*() functions are best for modifying state “from now until the function exits”
neat_local <- function(x, sig_digits) {
  withr::local_options(list(digits = sig_digits))
  print(x)
  # imagine lots of code here
}
  • with_*() functions are best for executing a small snippet of code with a modified state and minimize the footprint of your state modifications.
neat_with <- function(x, sig_digits) {
  # imagine lots of code here
  withr::with_options(
    list(digits = sig_digits),
    print(x)
  )
  # ... and a lot more code here
}