forked from laRusers/socalr2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCondition Handling.Rmd
148 lines (117 loc) · 2.43 KB
/
Condition Handling.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
---
title: "Condition System in R"
subtitle: "SoCal R Users Groups & R-Ladies All-hands Meeting"
author: "John Peach"
date: "2019-11-23"
output:
xaringan::moon_reader:
css: [default, metropolis, metropolis-fonts, intro.css, LSP.css]
lib_dir: libs
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
---
```{r setup, include=FALSE}
options(htmltools.dir.version = FALSE)
options(servr.daemon = TRUE)
```
# Condition System
.large[
- `message()`, `warning()`, `stop()`, `try()` and `tryCatch()` are built on the Condition System
- These are used for error handling
- Condition System can do much more
]
---
# Sequence of execution
![exception](exception.png)
---
# Components
.large[
1. Signaller
1. Handler
1. Restarting
]
---
# Components - Condition Object
.large[
- An S3 object
- must have the class `condition`
```
structure(
class = c(condition_class, "condition"),
list(message = message, call = call, ...)
)
```
]
---
# Components - Signaller
.large[
- `signalCondition()` raises a condition object
`
signalCondition(skip_entry_condition(text))
`
]
---
# Components - Handler
.large[
- `withCallHandlers()` catches the raised condition
```
withCallingHandlers(
{some code},
condition_class_1 = handler_function_1,
condition_class_2 = handler_function_2
)
```
```
withCallingHandlers(
parse_file(file),
skip_entry = skip_entry_handler,
keep_entry = keep_entry_handler
)
```
]
---
# Components - Restart
.large[
- `findRestart()` finds a restart label
- `invokeRestart()` transfer control to the restart
```
condition_handler_factory <- function(restart_name) {
function(cond) {
restart <- findRestart(restart_name)
if (is.null(restart)) { return() }
invokeRestart(restart)
}
}
```
]
---
# Components - Restart
.large[
- `withRestart()` entry point for restart
```
withRestarts(
{some code},
restart_name_1 = restart_function_1,
restart_name_2 = restart_function_2
)
```
```
withRestarts(
choose_entry_test(text, p_skip = 0.5),
skip_entry = function() NULL,
keep_entry = function() parse_entry(text)
)
```
]
---
# Call Stack
![condition_call_stack](condition_call_stack.png)
---
# Thank You
## References
* http://www.gigamonkeys.com/book/beyond-exception-handling-conditions-and-restarts.html
* https://adv-r.hadley.nz/conditions.html
* http://adv-r.had.co.nz/beyond-exception-handling.html
* http://adv-r.had.co.nz/Exceptions-Debugging.html