6.12 Restore state with withr::defer()

withr::defer() is basically a drop-in substitute for on.exit().

pi
#> [1] 3.141593

neater <- function(x, sig_digits) {
  op <- options(digits = sig_digits)
  defer(options(op))
  print(x)
}

neater(pi, 2)
#> [1] 3.1

pi
#> [1] 3.141593