Debian Codegraph @

inst/doc/withr.R

raw
1 ## ----include = FALSE----------------------------------------------------------
2 knitr::opts_chunk$set(
3 collapse = TRUE,
4 comment = "#>"
5 )
6
7 ## ----setup--------------------------------------------------------------------
8 library(withr)
9
10 ## ----include = FALSE----------------------------------------------------------
11 op <- options()
12
13 ## -----------------------------------------------------------------------------
14 sloppy <- function(x, sig_digits) {
15 options(digits = sig_digits)
16 print(x)
17 }
18
19 pi
20
21 sloppy(pi, 2)
22
23 pi
24
25 ## ----include = FALSE----------------------------------------------------------
26 options(op)
27
28 ## -----------------------------------------------------------------------------
29 neat <- function(x, sig_digits) {
30 op <- options(digits = sig_digits)
31 on.exit(options(op), add = TRUE)
32 print(x)
33 }
34
35 pi
36
37 neat(pi, 2)
38
39 pi
40
41 ## -----------------------------------------------------------------------------
42 neater <- function(x, sig_digits) {
43 op <- options(digits = sig_digits)
44 defer(options(op))
45 print(x)
46 }
47
48 pi
49
50 neater(pi, 2)
51
52 pi
53
54 ## -----------------------------------------------------------------------------
55 defer_stack <- function() {
56 cat("put on socks\n")
57 defer(cat("take off socks\n"))
58
59 cat("put on shoes\n")
60 defer(cat("take off shoes\n"))
61 }
62 defer_stack()
63
64 ## -----------------------------------------------------------------------------
65 on_exit_last_one_wins <- function() {
66 cat("put on socks\n")
67 on.exit(cat("take off socks\n"))
68
69 cat("put on shoes\n")
70 on.exit(cat("take off shoes\n"))
71 }
72 on_exit_last_one_wins()
73
74 ## ----eval = getRversion() >= "3.5.0"------------------------------------------
75 on_exit_stack <- function() {
76 cat("put on socks\n")
77 on.exit(cat("take off socks\n"), add = TRUE, after = FALSE)
78
79 cat("put on shoes\n")
80 on.exit(cat("take off shoes\n"), add = TRUE, after = FALSE)
81 }
82 on_exit_stack()
83
84 ## -----------------------------------------------------------------------------
85 defer_queue <- function() {
86 cat("Adam gets in line for ice cream\n")
87 defer(cat("Adam gets ice cream\n"), priority = "last")
88
89 cat("Beth gets in line for ice cream\n")
90 defer(cat("Beth gets ice cream\n"), priority = "last")
91 }
92 defer_queue()
93
94 ## -----------------------------------------------------------------------------
95 neater <- function(x, sig_digits) {
96 op <- options(digits = sig_digits) # record orig. "digits" & change "digits"
97 defer(options(op)) # schedule restoration of "digits"
98
99 print(x)
100 }
101
102 ## -----------------------------------------------------------------------------
103 local_digits <- function(sig_digits, envir = parent.frame()) {
104 op <- options(digits = sig_digits)
105 defer(options(op), envir = envir)
106 }
107
108 ## -----------------------------------------------------------------------------
109 neato <- function(x, digits) {
110 local_digits(digits)
111 print(x)
112 }
113
114 pi
115
116 neato(pi, 2)
117
118 neato(pi, 4)
119
120 ## -----------------------------------------------------------------------------
121 neatful <- function(x) {
122 local_digits(1)
123 print(x)
124 local_digits(3)
125 print(x)
126 local_digits(5)
127 print(x)
128 }
129
130 neatful(pi)
131
132 ## -----------------------------------------------------------------------------
133 neatest <- function(x, sig_digits) {
134 local_options(list(digits = sig_digits))
135 print(x)
136 }
137
138 pi
139
140 neatest(pi, 2)
141
142 neatest(pi, 4)
143
144 ## ----eval = FALSE-------------------------------------------------------------
145 # neat_with <- function(x, sig_digits) {
146 # # imagine lots of code here
147 # withr::with_options(
148 # list(digits = sig_digits),
149 # print(x)
150 # )
151 # # ... and a lot more code here
152 # }
153
154 ## ----eval = FALSE-------------------------------------------------------------
155 # neat_local <- function(x, sig_digits) {
156 # withr::local_options(list(digits = sig_digits))
157 # print(x)
158 # # imagine lots of code here
159 # }
160
161 ## -----------------------------------------------------------------------------
162 library(withr)
163
164 defer(print("hi"))
165
166 pi
167
168 # this adds another deferred event, but does not re-message
169 local_digits(3)
170
171 pi
172
173 deferred_run()
174
175 pi
176
177 ## ----eval = FALSE-------------------------------------------------------------
178 # defer(print("hi"))
179 # #> Setting global deferred event(s).
180 # #> i These will be run:
181 # #> * Automatically, when the R session ends.
182 # #> * On demand, if you call `withr::deferred_run()`.
183 # #> i Use `withr::deferred_clear()` to clear them without executing.
184