| 1 |
<!DOCTYPE html> |
| 2 |
|
| 3 |
<html> |
| 4 |
|
| 5 |
<head> |
| 6 |
|
| 7 |
<meta charset="utf-8" /> |
| 8 |
<meta name="generator" content="pandoc" /> |
| 9 |
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" /> |
| 10 |
|
| 11 |
<meta name="viewport" content="width=device-width, initial-scale=1" /> |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
<title>Classes and objects</title> |
| 16 |
|
| 17 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 18 |
// be compatible with the behavior of Pandoc < 2.8). |
| 19 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 20 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 21 |
var i, h, a; |
| 22 |
for (i = 0; i < hs.length; i++) { |
| 23 |
h = hs[i]; |
| 24 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 25 |
a = h.attributes; |
| 26 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 27 |
} |
| 28 |
}); |
| 29 |
</script> |
| 30 |
|
| 31 |
<style type="text/css"> |
| 32 |
code{white-space: pre-wrap;} |
| 33 |
span.smallcaps{font-variant: small-caps;} |
| 34 |
span.underline{text-decoration: underline;} |
| 35 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 36 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 37 |
ul.task-list{list-style: none;} |
| 38 |
</style> |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
<style type="text/css"> |
| 43 |
code { |
| 44 |
white-space: pre; |
| 45 |
} |
| 46 |
.sourceCode { |
| 47 |
overflow: visible; |
| 48 |
} |
| 49 |
</style> |
| 50 |
<style type="text/css" data-origin="pandoc"> |
| 51 |
html { -webkit-text-size-adjust: 100%; } |
| 52 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 53 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 54 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 55 |
.sourceCode { overflow: visible; } |
| 56 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 57 |
div.sourceCode { margin: 1em 0; } |
| 58 |
pre.sourceCode { margin: 0; } |
| 59 |
@media screen { |
| 60 |
div.sourceCode { overflow: auto; } |
| 61 |
} |
| 62 |
@media print { |
| 63 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 64 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 65 |
} |
| 66 |
pre.numberSource code |
| 67 |
{ counter-reset: source-line 0; } |
| 68 |
pre.numberSource code > span |
| 69 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 70 |
pre.numberSource code > span > a:first-child::before |
| 71 |
{ content: counter(source-line); |
| 72 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 73 |
border: none; display: inline-block; |
| 74 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 75 |
-khtml-user-select: none; -moz-user-select: none; |
| 76 |
-ms-user-select: none; user-select: none; |
| 77 |
padding: 0 4px; width: 4em; |
| 78 |
color: #aaaaaa; |
| 79 |
} |
| 80 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 81 |
div.sourceCode |
| 82 |
{ } |
| 83 |
@media screen { |
| 84 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 85 |
} |
| 86 |
code span.al { color: #ff0000; font-weight: bold; } |
| 87 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 88 |
code span.at { color: #7d9029; } |
| 89 |
code span.bn { color: #40a070; } |
| 90 |
code span.bu { color: #008000; } |
| 91 |
code span.cf { color: #007020; font-weight: bold; } |
| 92 |
code span.ch { color: #4070a0; } |
| 93 |
code span.cn { color: #880000; } |
| 94 |
code span.co { color: #60a0b0; font-style: italic; } |
| 95 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 96 |
code span.do { color: #ba2121; font-style: italic; } |
| 97 |
code span.dt { color: #902000; } |
| 98 |
code span.dv { color: #40a070; } |
| 99 |
code span.er { color: #ff0000; font-weight: bold; } |
| 100 |
code span.ex { } |
| 101 |
code span.fl { color: #40a070; } |
| 102 |
code span.fu { color: #06287e; } |
| 103 |
code span.im { color: #008000; font-weight: bold; } |
| 104 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 105 |
code span.kw { color: #007020; font-weight: bold; } |
| 106 |
code span.op { color: #666666; } |
| 107 |
code span.ot { color: #007020; } |
| 108 |
code span.pp { color: #bc7a00; } |
| 109 |
code span.sc { color: #4070a0; } |
| 110 |
code span.ss { color: #bb6688; } |
| 111 |
code span.st { color: #4070a0; } |
| 112 |
code span.va { color: #19177c; } |
| 113 |
code span.vs { color: #4070a0; } |
| 114 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 115 |
</style> |
| 116 |
<script> |
| 117 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 118 |
(function() { |
| 119 |
var sheets = document.styleSheets; |
| 120 |
for (var i = 0; i < sheets.length; i++) { |
| 121 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 122 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 123 |
var j = 0; |
| 124 |
while (j < rules.length) { |
| 125 |
var rule = rules[j]; |
| 126 |
// check if there is a div.sourceCode rule |
| 127 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 128 |
j++; |
| 129 |
continue; |
| 130 |
} |
| 131 |
var style = rule.style.cssText; |
| 132 |
// check if color or background-color is set |
| 133 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 134 |
j++; |
| 135 |
continue; |
| 136 |
} |
| 137 |
// replace div.sourceCode by a pre.sourceCode rule |
| 138 |
sheets[i].deleteRule(j); |
| 139 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 140 |
} |
| 141 |
} |
| 142 |
})(); |
| 143 |
</script> |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
<style type="text/css">body { |
| 149 |
background-color: #fff; |
| 150 |
margin: 1em auto; |
| 151 |
max-width: 700px; |
| 152 |
overflow: visible; |
| 153 |
padding-left: 2em; |
| 154 |
padding-right: 2em; |
| 155 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 156 |
font-size: 14px; |
| 157 |
line-height: 1.35; |
| 158 |
} |
| 159 |
#TOC { |
| 160 |
clear: both; |
| 161 |
margin: 0 0 10px 10px; |
| 162 |
padding: 4px; |
| 163 |
width: 400px; |
| 164 |
border: 1px solid #CCCCCC; |
| 165 |
border-radius: 5px; |
| 166 |
background-color: #f6f6f6; |
| 167 |
font-size: 13px; |
| 168 |
line-height: 1.3; |
| 169 |
} |
| 170 |
#TOC .toctitle { |
| 171 |
font-weight: bold; |
| 172 |
font-size: 15px; |
| 173 |
margin-left: 5px; |
| 174 |
} |
| 175 |
#TOC ul { |
| 176 |
padding-left: 40px; |
| 177 |
margin-left: -1.5em; |
| 178 |
margin-top: 5px; |
| 179 |
margin-bottom: 5px; |
| 180 |
} |
| 181 |
#TOC ul ul { |
| 182 |
margin-left: -2em; |
| 183 |
} |
| 184 |
#TOC li { |
| 185 |
line-height: 16px; |
| 186 |
} |
| 187 |
table { |
| 188 |
margin: 1em auto; |
| 189 |
border-width: 1px; |
| 190 |
border-color: #DDDDDD; |
| 191 |
border-style: outset; |
| 192 |
border-collapse: collapse; |
| 193 |
} |
| 194 |
table th { |
| 195 |
border-width: 2px; |
| 196 |
padding: 5px; |
| 197 |
border-style: inset; |
| 198 |
} |
| 199 |
table td { |
| 200 |
border-width: 1px; |
| 201 |
border-style: inset; |
| 202 |
line-height: 18px; |
| 203 |
padding: 5px 5px; |
| 204 |
} |
| 205 |
table, table th, table td { |
| 206 |
border-left-style: none; |
| 207 |
border-right-style: none; |
| 208 |
} |
| 209 |
table thead, table tr.even { |
| 210 |
background-color: #f7f7f7; |
| 211 |
} |
| 212 |
p { |
| 213 |
margin: 0.5em 0; |
| 214 |
} |
| 215 |
blockquote { |
| 216 |
background-color: #f6f6f6; |
| 217 |
padding: 0.25em 0.75em; |
| 218 |
} |
| 219 |
hr { |
| 220 |
border-style: solid; |
| 221 |
border: none; |
| 222 |
border-top: 1px solid #777; |
| 223 |
margin: 28px 0; |
| 224 |
} |
| 225 |
dl { |
| 226 |
margin-left: 0; |
| 227 |
} |
| 228 |
dl dd { |
| 229 |
margin-bottom: 13px; |
| 230 |
margin-left: 13px; |
| 231 |
} |
| 232 |
dl dt { |
| 233 |
font-weight: bold; |
| 234 |
} |
| 235 |
ul { |
| 236 |
margin-top: 0; |
| 237 |
} |
| 238 |
ul li { |
| 239 |
list-style: circle outside; |
| 240 |
} |
| 241 |
ul ul { |
| 242 |
margin-bottom: 0; |
| 243 |
} |
| 244 |
pre, code { |
| 245 |
background-color: #f7f7f7; |
| 246 |
border-radius: 3px; |
| 247 |
color: #333; |
| 248 |
white-space: pre-wrap; |
| 249 |
} |
| 250 |
pre { |
| 251 |
border-radius: 3px; |
| 252 |
margin: 5px 0px 10px 0px; |
| 253 |
padding: 10px; |
| 254 |
} |
| 255 |
pre:not([class]) { |
| 256 |
background-color: #f7f7f7; |
| 257 |
} |
| 258 |
code { |
| 259 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 260 |
font-size: 85%; |
| 261 |
} |
| 262 |
p > code, li > code { |
| 263 |
padding: 2px 0px; |
| 264 |
} |
| 265 |
div.figure { |
| 266 |
text-align: center; |
| 267 |
} |
| 268 |
img { |
| 269 |
background-color: #FFFFFF; |
| 270 |
padding: 2px; |
| 271 |
border: 1px solid #DDDDDD; |
| 272 |
border-radius: 3px; |
| 273 |
border: 1px solid #CCCCCC; |
| 274 |
margin: 0 5px; |
| 275 |
} |
| 276 |
h1 { |
| 277 |
margin-top: 0; |
| 278 |
font-size: 35px; |
| 279 |
line-height: 40px; |
| 280 |
} |
| 281 |
h2 { |
| 282 |
border-bottom: 4px solid #f7f7f7; |
| 283 |
padding-top: 10px; |
| 284 |
padding-bottom: 2px; |
| 285 |
font-size: 145%; |
| 286 |
} |
| 287 |
h3 { |
| 288 |
border-bottom: 2px solid #f7f7f7; |
| 289 |
padding-top: 10px; |
| 290 |
font-size: 120%; |
| 291 |
} |
| 292 |
h4 { |
| 293 |
border-bottom: 1px solid #f7f7f7; |
| 294 |
margin-left: 8px; |
| 295 |
font-size: 105%; |
| 296 |
} |
| 297 |
h5, h6 { |
| 298 |
border-bottom: 1px solid #ccc; |
| 299 |
font-size: 105%; |
| 300 |
} |
| 301 |
a { |
| 302 |
color: #0033dd; |
| 303 |
text-decoration: none; |
| 304 |
} |
| 305 |
a:hover { |
| 306 |
color: #6666ff; } |
| 307 |
a:visited { |
| 308 |
color: #800080; } |
| 309 |
a:visited:hover { |
| 310 |
color: #BB00BB; } |
| 311 |
a[href^="http:"] { |
| 312 |
text-decoration: underline; } |
| 313 |
a[href^="https:"] { |
| 314 |
text-decoration: underline; } |
| 315 |
|
| 316 |
code > span.kw { color: #555; font-weight: bold; } |
| 317 |
code > span.dt { color: #902000; } |
| 318 |
code > span.dv { color: #40a070; } |
| 319 |
code > span.bn { color: #d14; } |
| 320 |
code > span.fl { color: #d14; } |
| 321 |
code > span.ch { color: #d14; } |
| 322 |
code > span.st { color: #d14; } |
| 323 |
code > span.co { color: #888888; font-style: italic; } |
| 324 |
code > span.ot { color: #007020; } |
| 325 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 326 |
code > span.fu { color: #900; font-weight: bold; } |
| 327 |
code > span.er { color: #a61717; background-color: #e3d2d2; } |
| 328 |
</style> |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
</head> |
| 334 |
|
| 335 |
<body> |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
<h1 class="title toc-ignore">Classes and objects</h1> |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
<p>This vignette dives into the details of S7 classes and objects, |
| 345 |
building on the basics discussed in <code>vignette("S7")</code>. It will |
| 346 |
cover validators, the finer details of properties, and finally how to |
| 347 |
write your own constructors.</p> |
| 348 |
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(S7)</span></code></pre></div> |
| 349 |
<div id="validation" class="section level2"> |
| 350 |
<h2>Validation</h2> |
| 351 |
<p>S7 classes can have an optional <strong>validator</strong> that |
| 352 |
checks that the values of the properties are OK. A validator is a |
| 353 |
function that takes the object (called <code>self</code>) and returns |
| 354 |
<code>NULL</code> if it’s valid or returns a character vector listing |
| 355 |
the problems.</p> |
| 356 |
<div id="basics" class="section level3"> |
| 357 |
<h3>Basics</h3> |
| 358 |
<p>In the following example we create a <code>Range</code> class that |
| 359 |
enforces that <code>@start</code> and <code>@end</code> are single |
| 360 |
numbers, and that <code>@start</code> is less than |
| 361 |
<code>@end</code>:</p> |
| 362 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>Range <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Range"</span>,</span> |
| 363 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 364 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a> <span class="at">start =</span> class_double,</span> |
| 365 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> <span class="at">end =</span> class_double</span> |
| 366 |
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a> ),</span> |
| 367 |
<span id="cb2-6"><a href="#cb2-6" tabindex="-1"></a> <span class="at">validator =</span> <span class="cf">function</span>(self) {</span> |
| 368 |
<span id="cb2-7"><a href="#cb2-7" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(self<span class="sc">@</span>start) <span class="sc">!=</span> <span class="dv">1</span>) {</span> |
| 369 |
<span id="cb2-8"><a href="#cb2-8" tabindex="-1"></a> <span class="st">"@start must be length 1"</span></span> |
| 370 |
<span id="cb2-9"><a href="#cb2-9" tabindex="-1"></a> } <span class="cf">else</span> <span class="cf">if</span> (<span class="fu">length</span>(self<span class="sc">@</span>end) <span class="sc">!=</span> <span class="dv">1</span>) {</span> |
| 371 |
<span id="cb2-10"><a href="#cb2-10" tabindex="-1"></a> <span class="st">"@end must be length 1"</span></span> |
| 372 |
<span id="cb2-11"><a href="#cb2-11" tabindex="-1"></a> } <span class="cf">else</span> <span class="cf">if</span> (self<span class="sc">@</span>end <span class="sc"><</span> self<span class="sc">@</span>start) {</span> |
| 373 |
<span id="cb2-12"><a href="#cb2-12" tabindex="-1"></a> <span class="fu">sprintf</span>(</span> |
| 374 |
<span id="cb2-13"><a href="#cb2-13" tabindex="-1"></a> <span class="st">"@end (%i) must be greater than or equal to @start (%i)"</span>,</span> |
| 375 |
<span id="cb2-14"><a href="#cb2-14" tabindex="-1"></a> self<span class="sc">@</span>end,</span> |
| 376 |
<span id="cb2-15"><a href="#cb2-15" tabindex="-1"></a> self<span class="sc">@</span>start</span> |
| 377 |
<span id="cb2-16"><a href="#cb2-16" tabindex="-1"></a> )</span> |
| 378 |
<span id="cb2-17"><a href="#cb2-17" tabindex="-1"></a> }</span> |
| 379 |
<span id="cb2-18"><a href="#cb2-18" tabindex="-1"></a> }</span> |
| 380 |
<span id="cb2-19"><a href="#cb2-19" tabindex="-1"></a>)</span></code></pre></div> |
| 381 |
<p>You can typically write a validator as a series of |
| 382 |
<code>if</code>-<code>else</code> statements, but note that the order of |
| 383 |
the statements is important. For example, in the code above, we can’t |
| 384 |
check that <code>self@end < self@start</code> before we’ve checked |
| 385 |
that <code>@start</code> and <code>@end</code> are length 1.</p> |
| 386 |
<p>As we’ll discuss shortly, you can also perform validation on a |
| 387 |
per-property basis, so generally class validators should be reserved for |
| 388 |
interactions between properties.</p> |
| 389 |
</div> |
| 390 |
<div id="when-is-validation-performed" class="section level3"> |
| 391 |
<h3>When is validation performed?</h3> |
| 392 |
<p>Objects are validated automatically when constructed and when any |
| 393 |
property is modified:</p> |
| 394 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">Range</span>(<span class="dv">1</span>, <span class="dv">2</span><span class="sc">:</span><span class="dv">3</span>)</span> |
| 395 |
<span id="cb3-2"><a href="#cb3-2" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 396 |
<span id="cb3-3"><a href="#cb3-3" tabindex="-1"></a><span class="co">#> ! <Range> object properties are invalid:</span></span> |
| 397 |
<span id="cb3-4"><a href="#cb3-4" tabindex="-1"></a><span class="co">#> - @end must be <double>, not <integer></span></span> |
| 398 |
<span id="cb3-5"><a href="#cb3-5" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">Range</span>(<span class="dv">10</span>, <span class="dv">1</span>)</span> |
| 399 |
<span id="cb3-6"><a href="#cb3-6" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 400 |
<span id="cb3-7"><a href="#cb3-7" tabindex="-1"></a><span class="co">#> ! <Range> object is invalid:</span></span> |
| 401 |
<span id="cb3-8"><a href="#cb3-8" tabindex="-1"></a><span class="co">#> - @end (1) must be greater than or equal to @start (10)</span></span> |
| 402 |
<span id="cb3-9"><a href="#cb3-9" tabindex="-1"></a></span> |
| 403 |
<span id="cb3-10"><a href="#cb3-10" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">Range</span>(<span class="dv">1</span>, <span class="dv">10</span>)</span> |
| 404 |
<span id="cb3-11"><a href="#cb3-11" tabindex="-1"></a>x<span class="sc">@</span>start <span class="ot"><-</span> <span class="dv">20</span></span> |
| 405 |
<span id="cb3-12"><a href="#cb3-12" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 406 |
<span id="cb3-13"><a href="#cb3-13" tabindex="-1"></a><span class="co">#> ! <Range> object is invalid:</span></span> |
| 407 |
<span id="cb3-14"><a href="#cb3-14" tabindex="-1"></a><span class="co">#> - @end (10) must be greater than or equal to @start (20)</span></span></code></pre></div> |
| 408 |
<p>You can also manually <code>validate()</code> an object if you use a |
| 409 |
low-level R function to bypass the usual checks and balances of |
| 410 |
<code>@</code>:</p> |
| 411 |
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">Range</span>(<span class="dv">1</span>, <span class="dv">2</span>)</span> |
| 412 |
<span id="cb4-2"><a href="#cb4-2" tabindex="-1"></a><span class="fu">attr</span>(x, <span class="st">"start"</span>) <span class="ot"><-</span> <span class="dv">3</span></span> |
| 413 |
<span id="cb4-3"><a href="#cb4-3" tabindex="-1"></a><span class="fu">validate</span>(x)</span> |
| 414 |
<span id="cb4-4"><a href="#cb4-4" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 415 |
<span id="cb4-5"><a href="#cb4-5" tabindex="-1"></a><span class="co">#> ! <Range> object is invalid:</span></span> |
| 416 |
<span id="cb4-6"><a href="#cb4-6" tabindex="-1"></a><span class="co">#> - @end (2) must be greater than or equal to @start (3)</span></span></code></pre></div> |
| 417 |
</div> |
| 418 |
<div id="avoiding-validation" class="section level3"> |
| 419 |
<h3>Avoiding validation</h3> |
| 420 |
<p>Imagine you wanted to write a function that would shift a property to |
| 421 |
the left or the right:</p> |
| 422 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a>shift <span class="ot"><-</span> <span class="cf">function</span>(x, shift) {</span> |
| 423 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a> x<span class="sc">@</span>start <span class="ot"><-</span> x<span class="sc">@</span>start <span class="sc">+</span> shift</span> |
| 424 |
<span id="cb5-3"><a href="#cb5-3" tabindex="-1"></a> x<span class="sc">@</span>end <span class="ot"><-</span> x<span class="sc">@</span>end <span class="sc">+</span> shift</span> |
| 425 |
<span id="cb5-4"><a href="#cb5-4" tabindex="-1"></a> x</span> |
| 426 |
<span id="cb5-5"><a href="#cb5-5" tabindex="-1"></a>}</span> |
| 427 |
<span id="cb5-6"><a href="#cb5-6" tabindex="-1"></a><span class="fu">shift</span>(<span class="fu">Range</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">1</span>)</span> |
| 428 |
<span id="cb5-7"><a href="#cb5-7" tabindex="-1"></a><span class="co">#> <Range></span></span> |
| 429 |
<span id="cb5-8"><a href="#cb5-8" tabindex="-1"></a><span class="co">#> @ start: num 2</span></span> |
| 430 |
<span id="cb5-9"><a href="#cb5-9" tabindex="-1"></a><span class="co">#> @ end : num 11</span></span></code></pre></div> |
| 431 |
<p>There’s a problem if <code>shift</code> is larger than |
| 432 |
<code>@end</code> - <code>@start</code>:</p> |
| 433 |
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">shift</span>(<span class="fu">Range</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">10</span>)</span> |
| 434 |
<span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 435 |
<span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#> ! <Range> object is invalid:</span></span> |
| 436 |
<span id="cb6-4"><a href="#cb6-4" tabindex="-1"></a><span class="co">#> - @end (10) must be greater than or equal to @start (11)</span></span></code></pre></div> |
| 437 |
<p>While the end result of <code>shift()</code> will be valid, an |
| 438 |
intermediate state is not. The easiest way to resolve this problem is to |
| 439 |
set the properties all at once:</p> |
| 440 |
<div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a>shift <span class="ot"><-</span> <span class="cf">function</span>(x, shift) {</span> |
| 441 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a> <span class="fu">props</span>(x) <span class="ot"><-</span> <span class="fu">list</span>(</span> |
| 442 |
<span id="cb7-3"><a href="#cb7-3" tabindex="-1"></a> <span class="at">start =</span> x<span class="sc">@</span>start <span class="sc">+</span> shift,</span> |
| 443 |
<span id="cb7-4"><a href="#cb7-4" tabindex="-1"></a> <span class="at">end =</span> x<span class="sc">@</span>end <span class="sc">+</span> shift</span> |
| 444 |
<span id="cb7-5"><a href="#cb7-5" tabindex="-1"></a> )</span> |
| 445 |
<span id="cb7-6"><a href="#cb7-6" tabindex="-1"></a> x</span> |
| 446 |
<span id="cb7-7"><a href="#cb7-7" tabindex="-1"></a>}</span> |
| 447 |
<span id="cb7-8"><a href="#cb7-8" tabindex="-1"></a><span class="fu">shift</span>(<span class="fu">Range</span>(<span class="dv">1</span>, <span class="dv">10</span>), <span class="dv">10</span>)</span> |
| 448 |
<span id="cb7-9"><a href="#cb7-9" tabindex="-1"></a><span class="co">#> <Range></span></span> |
| 449 |
<span id="cb7-10"><a href="#cb7-10" tabindex="-1"></a><span class="co">#> @ start: num 11</span></span> |
| 450 |
<span id="cb7-11"><a href="#cb7-11" tabindex="-1"></a><span class="co">#> @ end : num 20</span></span></code></pre></div> |
| 451 |
<p>The object is still validated, but it’s only validated once, after |
| 452 |
all the properties have been modified.</p> |
| 453 |
</div> |
| 454 |
</div> |
| 455 |
<div id="properties" class="section level2"> |
| 456 |
<h2>Properties</h2> |
| 457 |
<p>So far we’ve focused on the simplest form of property specification |
| 458 |
where you use a named list to supply the desired type for each property. |
| 459 |
This is a convenient shorthand for a call to |
| 460 |
<code>new_property()</code>. For example, the property definition of |
| 461 |
range above is shorthand for:</p> |
| 462 |
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>Range <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Range"</span>,</span> |
| 463 |
<span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 464 |
<span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> <span class="at">start =</span> <span class="fu">new_property</span>(class_double),</span> |
| 465 |
<span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a> <span class="at">end =</span> <span class="fu">new_property</span>(class_double)</span> |
| 466 |
<span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a> )</span> |
| 467 |
<span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a>)</span></code></pre></div> |
| 468 |
<p>Calling <code>new_property()</code> explicitly allows you to control |
| 469 |
aspects of the property other than its type. The following sections show |
| 470 |
you how to add a validator, provide a default value, compute the |
| 471 |
property value on demand, or provide a fully dynamic property.</p> |
| 472 |
<div id="validation-1" class="section level3"> |
| 473 |
<h3>Validation</h3> |
| 474 |
<p>You can optionally provide a validator for each property. For |
| 475 |
example, instead of validating the length of <code>start</code> and |
| 476 |
<code>end</code> in the validator of our <code>Range</code> class, we |
| 477 |
could implement those at the property level:</p> |
| 478 |
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>prop_number <span class="ot"><-</span> <span class="fu">new_property</span>(</span> |
| 479 |
<span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> <span class="at">class =</span> class_double,</span> |
| 480 |
<span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="at">validator =</span> <span class="cf">function</span>(value) {</span> |
| 481 |
<span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(value) <span class="sc">!=</span> <span class="dv">1</span><span class="dt">L</span>) <span class="st">"must be length 1"</span></span> |
| 482 |
<span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a> }</span> |
| 483 |
<span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a>)</span> |
| 484 |
<span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a></span> |
| 485 |
<span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a>Range <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Range"</span>,</span> |
| 486 |
<span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 487 |
<span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a> <span class="at">start =</span> prop_number,</span> |
| 488 |
<span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a> <span class="at">end =</span> prop_number</span> |
| 489 |
<span id="cb9-12"><a href="#cb9-12" tabindex="-1"></a> ),</span> |
| 490 |
<span id="cb9-13"><a href="#cb9-13" tabindex="-1"></a> <span class="at">validator =</span> <span class="cf">function</span>(self) {</span> |
| 491 |
<span id="cb9-14"><a href="#cb9-14" tabindex="-1"></a> <span class="cf">if</span> (self<span class="sc">@</span>end <span class="sc"><</span> self<span class="sc">@</span>start) {</span> |
| 492 |
<span id="cb9-15"><a href="#cb9-15" tabindex="-1"></a> <span class="fu">sprintf</span>(</span> |
| 493 |
<span id="cb9-16"><a href="#cb9-16" tabindex="-1"></a> <span class="st">"@end (%i) must be greater than or equal to @start (%i)"</span>,</span> |
| 494 |
<span id="cb9-17"><a href="#cb9-17" tabindex="-1"></a> self<span class="sc">@</span>end,</span> |
| 495 |
<span id="cb9-18"><a href="#cb9-18" tabindex="-1"></a> self<span class="sc">@</span>start</span> |
| 496 |
<span id="cb9-19"><a href="#cb9-19" tabindex="-1"></a> )</span> |
| 497 |
<span id="cb9-20"><a href="#cb9-20" tabindex="-1"></a> }</span> |
| 498 |
<span id="cb9-21"><a href="#cb9-21" tabindex="-1"></a> }</span> |
| 499 |
<span id="cb9-22"><a href="#cb9-22" tabindex="-1"></a>)</span> |
| 500 |
<span id="cb9-23"><a href="#cb9-23" tabindex="-1"></a></span> |
| 501 |
<span id="cb9-24"><a href="#cb9-24" tabindex="-1"></a><span class="fu">Range</span>(<span class="at">start =</span> <span class="fu">c</span>(<span class="fl">1.5</span>, <span class="fl">3.5</span>))</span> |
| 502 |
<span id="cb9-25"><a href="#cb9-25" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 503 |
<span id="cb9-26"><a href="#cb9-26" tabindex="-1"></a><span class="co">#> ! <Range> object properties are invalid:</span></span> |
| 504 |
<span id="cb9-27"><a href="#cb9-27" tabindex="-1"></a><span class="co">#> - @start must be length 1</span></span> |
| 505 |
<span id="cb9-28"><a href="#cb9-28" tabindex="-1"></a><span class="co">#> - @end must be length 1</span></span> |
| 506 |
<span id="cb9-29"><a href="#cb9-29" tabindex="-1"></a><span class="fu">Range</span>(<span class="at">end =</span> <span class="fu">c</span>(<span class="fl">1.5</span>, <span class="fl">3.5</span>))</span> |
| 507 |
<span id="cb9-30"><a href="#cb9-30" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 508 |
<span id="cb9-31"><a href="#cb9-31" tabindex="-1"></a><span class="co">#> ! <Range> object properties are invalid:</span></span> |
| 509 |
<span id="cb9-32"><a href="#cb9-32" tabindex="-1"></a><span class="co">#> - @start must be length 1</span></span> |
| 510 |
<span id="cb9-33"><a href="#cb9-33" tabindex="-1"></a><span class="co">#> - @end must be length 1</span></span></code></pre></div> |
| 511 |
<p>Note that property validators shouldn’t include the name of the |
| 512 |
property in validation messages as S7 will add it automatically. This |
| 513 |
makes it possible to use the same property definition for multiple |
| 514 |
properties of the same type, as above.</p> |
| 515 |
</div> |
| 516 |
<div id="default-value" class="section level3"> |
| 517 |
<h3>Default value</h3> |
| 518 |
<p>The defaults of <code>new_class()</code> create a class that can be |
| 519 |
constructed with no arguments:</p> |
| 520 |
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a>Empty <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Empty"</span>,</span> |
| 521 |
<span id="cb10-2"><a href="#cb10-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 522 |
<span id="cb10-3"><a href="#cb10-3" tabindex="-1"></a> <span class="at">x =</span> class_double,</span> |
| 523 |
<span id="cb10-4"><a href="#cb10-4" tabindex="-1"></a> <span class="at">y =</span> class_character,</span> |
| 524 |
<span id="cb10-5"><a href="#cb10-5" tabindex="-1"></a> <span class="at">z =</span> class_logical</span> |
| 525 |
<span id="cb10-6"><a href="#cb10-6" tabindex="-1"></a> ))</span> |
| 526 |
<span id="cb10-7"><a href="#cb10-7" tabindex="-1"></a><span class="fu">Empty</span>()</span> |
| 527 |
<span id="cb10-8"><a href="#cb10-8" tabindex="-1"></a><span class="co">#> <Empty></span></span> |
| 528 |
<span id="cb10-9"><a href="#cb10-9" tabindex="-1"></a><span class="co">#> @ x: num(0) </span></span> |
| 529 |
<span id="cb10-10"><a href="#cb10-10" tabindex="-1"></a><span class="co">#> @ y: chr(0) </span></span> |
| 530 |
<span id="cb10-11"><a href="#cb10-11" tabindex="-1"></a><span class="co">#> @ z: logi(0)</span></span></code></pre></div> |
| 531 |
<p>The default values of the properties will be filled in with “empty” |
| 532 |
instances. You can instead provide your own defaults by using the |
| 533 |
<code>default</code> argument:</p> |
| 534 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>Empty <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Empty"</span>,</span> |
| 535 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 536 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a> <span class="at">x =</span> <span class="fu">new_property</span>(class_numeric, <span class="at">default =</span> <span class="dv">0</span>),</span> |
| 537 |
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a> <span class="at">y =</span> <span class="fu">new_property</span>(class_character, <span class="at">default =</span> <span class="st">""</span>),</span> |
| 538 |
<span id="cb11-5"><a href="#cb11-5" tabindex="-1"></a> <span class="at">z =</span> <span class="fu">new_property</span>(class_logical, <span class="at">default =</span> <span class="cn">NA</span>)</span> |
| 539 |
<span id="cb11-6"><a href="#cb11-6" tabindex="-1"></a> )</span> |
| 540 |
<span id="cb11-7"><a href="#cb11-7" tabindex="-1"></a>)</span> |
| 541 |
<span id="cb11-8"><a href="#cb11-8" tabindex="-1"></a><span class="fu">Empty</span>()</span> |
| 542 |
<span id="cb11-9"><a href="#cb11-9" tabindex="-1"></a><span class="co">#> <Empty></span></span> |
| 543 |
<span id="cb11-10"><a href="#cb11-10" tabindex="-1"></a><span class="co">#> @ x: num 0</span></span> |
| 544 |
<span id="cb11-11"><a href="#cb11-11" tabindex="-1"></a><span class="co">#> @ y: chr ""</span></span> |
| 545 |
<span id="cb11-12"><a href="#cb11-12" tabindex="-1"></a><span class="co">#> @ z: logi NA</span></span></code></pre></div> |
| 546 |
<p>A quoted call becomes a standard function promise in the default |
| 547 |
constructor, evaluated at the time the object is constructed.</p> |
| 548 |
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb12-1"><a href="#cb12-1" tabindex="-1"></a>Stopwatch <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Stopwatch"</span>, <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 549 |
<span id="cb12-2"><a href="#cb12-2" tabindex="-1"></a> <span class="at">start_time =</span> <span class="fu">new_property</span>(</span> |
| 550 |
<span id="cb12-3"><a href="#cb12-3" tabindex="-1"></a> <span class="at">class =</span> class_POSIXct,</span> |
| 551 |
<span id="cb12-4"><a href="#cb12-4" tabindex="-1"></a> <span class="at">default =</span> <span class="fu">quote</span>(<span class="fu">Sys.time</span>())</span> |
| 552 |
<span id="cb12-5"><a href="#cb12-5" tabindex="-1"></a> ),</span> |
| 553 |
<span id="cb12-6"><a href="#cb12-6" tabindex="-1"></a> <span class="at">elapsed =</span> <span class="fu">new_property</span>(</span> |
| 554 |
<span id="cb12-7"><a href="#cb12-7" tabindex="-1"></a> <span class="at">getter =</span> <span class="cf">function</span>(self) {</span> |
| 555 |
<span id="cb12-8"><a href="#cb12-8" tabindex="-1"></a> <span class="fu">difftime</span>(<span class="fu">Sys.time</span>(), self<span class="sc">@</span>start_time, <span class="at">units =</span> <span class="st">"secs"</span>)</span> |
| 556 |
<span id="cb12-9"><a href="#cb12-9" tabindex="-1"></a> }</span> |
| 557 |
<span id="cb12-10"><a href="#cb12-10" tabindex="-1"></a> )</span> |
| 558 |
<span id="cb12-11"><a href="#cb12-11" tabindex="-1"></a>))</span> |
| 559 |
<span id="cb12-12"><a href="#cb12-12" tabindex="-1"></a><span class="fu">args</span>(Stopwatch)</span> |
| 560 |
<span id="cb12-13"><a href="#cb12-13" tabindex="-1"></a><span class="co">#> function (start_time = Sys.time()) </span></span> |
| 561 |
<span id="cb12-14"><a href="#cb12-14" tabindex="-1"></a><span class="co">#> NULL</span></span> |
| 562 |
<span id="cb12-15"><a href="#cb12-15" tabindex="-1"></a><span class="fu">round</span>(<span class="fu">Stopwatch</span>()<span class="sc">@</span>elapsed)</span> |
| 563 |
<span id="cb12-16"><a href="#cb12-16" tabindex="-1"></a><span class="co">#> Time difference of 0 secs</span></span> |
| 564 |
<span id="cb12-17"><a href="#cb12-17" tabindex="-1"></a><span class="fu">round</span>(<span class="fu">Stopwatch</span>(<span class="fu">Sys.time</span>() <span class="sc">-</span> <span class="dv">1</span>)<span class="sc">@</span>elapsed)</span> |
| 565 |
<span id="cb12-18"><a href="#cb12-18" tabindex="-1"></a><span class="co">#> Time difference of 1 secs</span></span></code></pre></div> |
| 566 |
</div> |
| 567 |
<div id="computed-properties" class="section level3"> |
| 568 |
<h3>Computed properties</h3> |
| 569 |
<p>It’s sometimes useful to have a property that is computed on demand. |
| 570 |
For example, it’d be convenient to pretend that our range has a length, |
| 571 |
which is just the distance between <code>@start</code> and |
| 572 |
<code>@end</code>. You can dynamically compute the value of a property |
| 573 |
by defining a <code>getter</code>:</p> |
| 574 |
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>Range <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Range"</span>,</span> |
| 575 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 576 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="at">start =</span> class_double,</span> |
| 577 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a> <span class="at">end =</span> class_double,</span> |
| 578 |
<span id="cb13-5"><a href="#cb13-5" tabindex="-1"></a> <span class="at">length =</span> <span class="fu">new_property</span>(</span> |
| 579 |
<span id="cb13-6"><a href="#cb13-6" tabindex="-1"></a> <span class="at">getter =</span> <span class="cf">function</span>(self) self<span class="sc">@</span>end <span class="sc">-</span> self<span class="sc">@</span>start</span> |
| 580 |
<span id="cb13-7"><a href="#cb13-7" tabindex="-1"></a> )</span> |
| 581 |
<span id="cb13-8"><a href="#cb13-8" tabindex="-1"></a> )</span> |
| 582 |
<span id="cb13-9"><a href="#cb13-9" tabindex="-1"></a>)</span> |
| 583 |
<span id="cb13-10"><a href="#cb13-10" tabindex="-1"></a></span> |
| 584 |
<span id="cb13-11"><a href="#cb13-11" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">Range</span>(<span class="at">start =</span> <span class="dv">1</span>, <span class="at">end =</span> <span class="dv">10</span>)</span> |
| 585 |
<span id="cb13-12"><a href="#cb13-12" tabindex="-1"></a>x</span> |
| 586 |
<span id="cb13-13"><a href="#cb13-13" tabindex="-1"></a><span class="co">#> <Range></span></span> |
| 587 |
<span id="cb13-14"><a href="#cb13-14" tabindex="-1"></a><span class="co">#> @ start : num 1</span></span> |
| 588 |
<span id="cb13-15"><a href="#cb13-15" tabindex="-1"></a><span class="co">#> @ end : num 10</span></span> |
| 589 |
<span id="cb13-16"><a href="#cb13-16" tabindex="-1"></a><span class="co">#> @ length: num 9</span></span></code></pre></div> |
| 590 |
<p>Computed properties are read-only:</p> |
| 591 |
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb14-1"><a href="#cb14-1" tabindex="-1"></a>x<span class="sc">@</span>length <span class="ot"><-</span> <span class="dv">20</span></span> |
| 592 |
<span id="cb14-2"><a href="#cb14-2" tabindex="-1"></a><span class="co">#> Error:</span></span> |
| 593 |
<span id="cb14-3"><a href="#cb14-3" tabindex="-1"></a><span class="co">#> ! Can't set read-only property <Range>@length</span></span></code></pre></div> |
| 594 |
</div> |
| 595 |
<div id="dynamic-properties" class="section level3"> |
| 596 |
<h3>Dynamic properties</h3> |
| 597 |
<p>You can make a computed property fully dynamic so that it can be read |
| 598 |
and written by also supplying a <code>setter</code>.</p> |
| 599 |
<p>A <code>setter</code> is a function with arguments <code>self</code> |
| 600 |
and <code>value</code> that returns a modified object.</p> |
| 601 |
<p>For example, we could extend the previous example to allow the |
| 602 |
<code>@length</code> to be set, by modifying the <code>@end</code> of |
| 603 |
the vector:</p> |
| 604 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>Range <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Range"</span>,</span> |
| 605 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 606 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="at">start =</span> class_double,</span> |
| 607 |
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a> <span class="at">end =</span> class_double,</span> |
| 608 |
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a> <span class="at">length =</span> <span class="fu">new_property</span>(</span> |
| 609 |
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a> <span class="at">class =</span> class_double,</span> |
| 610 |
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a> <span class="at">getter =</span> <span class="cf">function</span>(self) self<span class="sc">@</span>end <span class="sc">-</span> self<span class="sc">@</span>start,</span> |
| 611 |
<span id="cb15-8"><a href="#cb15-8" tabindex="-1"></a> <span class="at">setter =</span> <span class="cf">function</span>(self, value) {</span> |
| 612 |
<span id="cb15-9"><a href="#cb15-9" tabindex="-1"></a> <span class="cf">if</span> (<span class="sc">!</span><span class="fu">length</span>(value)) {</span> |
| 613 |
<span id="cb15-10"><a href="#cb15-10" tabindex="-1"></a> <span class="co"># Do nothing if called with the constructor default</span></span> |
| 614 |
<span id="cb15-11"><a href="#cb15-11" tabindex="-1"></a> <span class="co"># value for this property, a zero-length double vector.</span></span> |
| 615 |
<span id="cb15-12"><a href="#cb15-12" tabindex="-1"></a> <span class="fu">return</span>(self)</span> |
| 616 |
<span id="cb15-13"><a href="#cb15-13" tabindex="-1"></a> }</span> |
| 617 |
<span id="cb15-14"><a href="#cb15-14" tabindex="-1"></a> self<span class="sc">@</span>end <span class="ot"><-</span> self<span class="sc">@</span>start <span class="sc">+</span> value</span> |
| 618 |
<span id="cb15-15"><a href="#cb15-15" tabindex="-1"></a> self</span> |
| 619 |
<span id="cb15-16"><a href="#cb15-16" tabindex="-1"></a> }</span> |
| 620 |
<span id="cb15-17"><a href="#cb15-17" tabindex="-1"></a> )</span> |
| 621 |
<span id="cb15-18"><a href="#cb15-18" tabindex="-1"></a> )</span> |
| 622 |
<span id="cb15-19"><a href="#cb15-19" tabindex="-1"></a>)</span> |
| 623 |
<span id="cb15-20"><a href="#cb15-20" tabindex="-1"></a></span> |
| 624 |
<span id="cb15-21"><a href="#cb15-21" tabindex="-1"></a>x <span class="ot"><-</span> <span class="fu">Range</span>(<span class="at">start =</span> <span class="dv">1</span>, <span class="at">end =</span> <span class="dv">10</span>)</span> |
| 625 |
<span id="cb15-22"><a href="#cb15-22" tabindex="-1"></a>x</span> |
| 626 |
<span id="cb15-23"><a href="#cb15-23" tabindex="-1"></a><span class="co">#> <Range></span></span> |
| 627 |
<span id="cb15-24"><a href="#cb15-24" tabindex="-1"></a><span class="co">#> @ start : num 1</span></span> |
| 628 |
<span id="cb15-25"><a href="#cb15-25" tabindex="-1"></a><span class="co">#> @ end : num 10</span></span> |
| 629 |
<span id="cb15-26"><a href="#cb15-26" tabindex="-1"></a><span class="co">#> @ length: num 9</span></span> |
| 630 |
<span id="cb15-27"><a href="#cb15-27" tabindex="-1"></a></span> |
| 631 |
<span id="cb15-28"><a href="#cb15-28" tabindex="-1"></a>x<span class="sc">@</span>length <span class="ot"><-</span> <span class="dv">5</span></span> |
| 632 |
<span id="cb15-29"><a href="#cb15-29" tabindex="-1"></a>x</span> |
| 633 |
<span id="cb15-30"><a href="#cb15-30" tabindex="-1"></a><span class="co">#> <Range></span></span> |
| 634 |
<span id="cb15-31"><a href="#cb15-31" tabindex="-1"></a><span class="co">#> @ start : num 1</span></span> |
| 635 |
<span id="cb15-32"><a href="#cb15-32" tabindex="-1"></a><span class="co">#> @ end : num 6</span></span> |
| 636 |
<span id="cb15-33"><a href="#cb15-33" tabindex="-1"></a><span class="co">#> @ length: num 5</span></span></code></pre></div> |
| 637 |
</div> |
| 638 |
<div id="common-patterns" class="section level3"> |
| 639 |
<h3>Common Patterns</h3> |
| 640 |
<p><code>getter</code>, <code>setter</code>, <code>default</code>, and |
| 641 |
<code>validator</code> can be used to implement many common patterns of |
| 642 |
properties.</p> |
| 643 |
<div id="deprecated-properties" class="section level4"> |
| 644 |
<h4>Deprecated properties</h4> |
| 645 |
<p>A <code>setter</code> + <code>getter</code> can be used to to |
| 646 |
deprecate a property:</p> |
| 647 |
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>Person <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Person"</span>, <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 648 |
<span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> <span class="at">first_name =</span> class_character,</span> |
| 649 |
<span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a> <span class="at">firstName =</span> <span class="fu">new_property</span>(</span> |
| 650 |
<span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> class_character,</span> |
| 651 |
<span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a> <span class="at">default =</span> <span class="fu">quote</span>(first_name),</span> |
| 652 |
<span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a> <span class="at">getter =</span> <span class="cf">function</span>(self) {</span> |
| 653 |
<span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a> <span class="fu">warning</span>(<span class="st">"@firstName is deprecated; please use @first_name instead"</span>, <span class="at">call. =</span> <span class="cn">FALSE</span>)</span> |
| 654 |
<span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a> self<span class="sc">@</span>first_name</span> |
| 655 |
<span id="cb16-9"><a href="#cb16-9" tabindex="-1"></a> },</span> |
| 656 |
<span id="cb16-10"><a href="#cb16-10" tabindex="-1"></a> <span class="at">setter =</span> <span class="cf">function</span>(self, value) {</span> |
| 657 |
<span id="cb16-11"><a href="#cb16-11" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">identical</span>(value, self<span class="sc">@</span>first_name)) {</span> |
| 658 |
<span id="cb16-12"><a href="#cb16-12" tabindex="-1"></a> <span class="fu">return</span>(self)</span> |
| 659 |
<span id="cb16-13"><a href="#cb16-13" tabindex="-1"></a> }</span> |
| 660 |
<span id="cb16-14"><a href="#cb16-14" tabindex="-1"></a> <span class="fu">warning</span>(<span class="st">"@firstName is deprecated; please use @first_name instead"</span>, <span class="at">call. =</span> <span class="cn">FALSE</span>)</span> |
| 661 |
<span id="cb16-15"><a href="#cb16-15" tabindex="-1"></a> self<span class="sc">@</span>first_name <span class="ot"><-</span> value</span> |
| 662 |
<span id="cb16-16"><a href="#cb16-16" tabindex="-1"></a> self</span> |
| 663 |
<span id="cb16-17"><a href="#cb16-17" tabindex="-1"></a> }</span> |
| 664 |
<span id="cb16-18"><a href="#cb16-18" tabindex="-1"></a> )</span> |
| 665 |
<span id="cb16-19"><a href="#cb16-19" tabindex="-1"></a>))</span> |
| 666 |
<span id="cb16-20"><a href="#cb16-20" tabindex="-1"></a></span> |
| 667 |
<span id="cb16-21"><a href="#cb16-21" tabindex="-1"></a><span class="fu">args</span>(Person)</span> |
| 668 |
<span id="cb16-22"><a href="#cb16-22" tabindex="-1"></a><span class="co">#> function (first_name = character(0), firstName = first_name) </span></span> |
| 669 |
<span id="cb16-23"><a href="#cb16-23" tabindex="-1"></a><span class="co">#> NULL</span></span> |
| 670 |
<span id="cb16-24"><a href="#cb16-24" tabindex="-1"></a></span> |
| 671 |
<span id="cb16-25"><a href="#cb16-25" tabindex="-1"></a>hadley <span class="ot"><-</span> <span class="fu">Person</span>(<span class="at">firstName =</span> <span class="st">"Hadley"</span>)</span> |
| 672 |
<span id="cb16-26"><a href="#cb16-26" tabindex="-1"></a><span class="co">#> Warning: @firstName is deprecated; please use @first_name instead</span></span> |
| 673 |
<span id="cb16-27"><a href="#cb16-27" tabindex="-1"></a></span> |
| 674 |
<span id="cb16-28"><a href="#cb16-28" tabindex="-1"></a>hadley <span class="ot"><-</span> <span class="fu">Person</span>(<span class="at">first_name =</span> <span class="st">"Hadley"</span>) <span class="co"># no warning</span></span> |
| 675 |
<span id="cb16-29"><a href="#cb16-29" tabindex="-1"></a></span> |
| 676 |
<span id="cb16-30"><a href="#cb16-30" tabindex="-1"></a>hadley<span class="sc">@</span>firstName</span> |
| 677 |
<span id="cb16-31"><a href="#cb16-31" tabindex="-1"></a><span class="co">#> Warning: @firstName is deprecated; please use @first_name instead</span></span> |
| 678 |
<span id="cb16-32"><a href="#cb16-32" tabindex="-1"></a><span class="co">#> [1] "Hadley"</span></span> |
| 679 |
<span id="cb16-33"><a href="#cb16-33" tabindex="-1"></a></span> |
| 680 |
<span id="cb16-34"><a href="#cb16-34" tabindex="-1"></a>hadley<span class="sc">@</span>firstName <span class="ot"><-</span> <span class="st">"John"</span></span> |
| 681 |
<span id="cb16-35"><a href="#cb16-35" tabindex="-1"></a><span class="co">#> Warning: @firstName is deprecated; please use @first_name instead</span></span> |
| 682 |
<span id="cb16-36"><a href="#cb16-36" tabindex="-1"></a></span> |
| 683 |
<span id="cb16-37"><a href="#cb16-37" tabindex="-1"></a>hadley<span class="sc">@</span>first_name <span class="co"># no warning</span></span> |
| 684 |
<span id="cb16-38"><a href="#cb16-38" tabindex="-1"></a><span class="co">#> [1] "John"</span></span></code></pre></div> |
| 685 |
</div> |
| 686 |
<div id="required-properties" class="section level4"> |
| 687 |
<h4>Required properties</h4> |
| 688 |
<p>You can make a property required by the constructor either by:</p> |
| 689 |
<ul> |
| 690 |
<li>relying on the validator to error with the default value, or by</li> |
| 691 |
<li>setting the property default to a quoted error call.</li> |
| 692 |
</ul> |
| 693 |
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a>Person <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Person"</span>, <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 694 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a> <span class="at">name =</span> <span class="fu">new_property</span>(</span> |
| 695 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a> class_character,</span> |
| 696 |
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a> <span class="at">validator =</span> <span class="cf">function</span>(value) {</span> |
| 697 |
<span id="cb17-5"><a href="#cb17-5" tabindex="-1"></a> <span class="cf">if</span> (<span class="fu">length</span>(value) <span class="sc">!=</span> <span class="dv">1</span> <span class="sc">||</span> <span class="fu">is.na</span>(value) <span class="sc">||</span> value <span class="sc">==</span> <span class="st">""</span>)</span> |
| 698 |
<span id="cb17-6"><a href="#cb17-6" tabindex="-1"></a> <span class="st">"must be a non-empty string"</span></span> |
| 699 |
<span id="cb17-7"><a href="#cb17-7" tabindex="-1"></a> }</span> |
| 700 |
<span id="cb17-8"><a href="#cb17-8" tabindex="-1"></a> )</span> |
| 701 |
<span id="cb17-9"><a href="#cb17-9" tabindex="-1"></a>))</span> |
| 702 |
<span id="cb17-10"><a href="#cb17-10" tabindex="-1"></a></span> |
| 703 |
<span id="cb17-11"><a href="#cb17-11" tabindex="-1"></a><span class="fu">try</span>(<span class="fu">Person</span>())</span> |
| 704 |
<span id="cb17-12"><a href="#cb17-12" tabindex="-1"></a><span class="co">#> Error : <Person> object properties are invalid:</span></span> |
| 705 |
<span id="cb17-13"><a href="#cb17-13" tabindex="-1"></a><span class="co">#> - @name must be a non-empty string</span></span> |
| 706 |
<span id="cb17-14"><a href="#cb17-14" tabindex="-1"></a></span> |
| 707 |
<span id="cb17-15"><a href="#cb17-15" tabindex="-1"></a><span class="fu">try</span>(<span class="fu">Person</span>(<span class="dv">1</span>)) <span class="co"># class_character$validator() is also checked.</span></span> |
| 708 |
<span id="cb17-16"><a href="#cb17-16" tabindex="-1"></a><span class="co">#> Error : <Person> object properties are invalid:</span></span> |
| 709 |
<span id="cb17-17"><a href="#cb17-17" tabindex="-1"></a><span class="co">#> - @name must be <character>, not <double></span></span> |
| 710 |
<span id="cb17-18"><a href="#cb17-18" tabindex="-1"></a></span> |
| 711 |
<span id="cb17-19"><a href="#cb17-19" tabindex="-1"></a><span class="fu">Person</span>(<span class="st">"Alice"</span>)</span> |
| 712 |
<span id="cb17-20"><a href="#cb17-20" tabindex="-1"></a><span class="co">#> <Person></span></span> |
| 713 |
<span id="cb17-21"><a href="#cb17-21" tabindex="-1"></a><span class="co">#> @ name: chr "Alice"</span></span></code></pre></div> |
| 714 |
<div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a>Person <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Person"</span>, <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 715 |
<span id="cb18-2"><a href="#cb18-2" tabindex="-1"></a> <span class="at">name =</span> <span class="fu">new_property</span>(</span> |
| 716 |
<span id="cb18-3"><a href="#cb18-3" tabindex="-1"></a> class_character,</span> |
| 717 |
<span id="cb18-4"><a href="#cb18-4" tabindex="-1"></a> <span class="at">default =</span> <span class="fu">quote</span>(<span class="fu">stop</span>(<span class="st">"@name is required"</span>)))</span> |
| 718 |
<span id="cb18-5"><a href="#cb18-5" tabindex="-1"></a>))</span> |
| 719 |
<span id="cb18-6"><a href="#cb18-6" tabindex="-1"></a></span> |
| 720 |
<span id="cb18-7"><a href="#cb18-7" tabindex="-1"></a><span class="fu">try</span>(<span class="fu">Person</span>())</span> |
| 721 |
<span id="cb18-8"><a href="#cb18-8" tabindex="-1"></a><span class="co">#> Error in Person() : @name is required</span></span> |
| 722 |
<span id="cb18-9"><a href="#cb18-9" tabindex="-1"></a></span> |
| 723 |
<span id="cb18-10"><a href="#cb18-10" tabindex="-1"></a><span class="fu">Person</span>(<span class="st">"Alice"</span>)</span> |
| 724 |
<span id="cb18-11"><a href="#cb18-11" tabindex="-1"></a><span class="co">#> <Person></span></span> |
| 725 |
<span id="cb18-12"><a href="#cb18-12" tabindex="-1"></a><span class="co">#> @ name: chr "Alice"</span></span></code></pre></div> |
| 726 |
</div> |
| 727 |
<div id="frozen-properties" class="section level4"> |
| 728 |
<h4>Frozen properties</h4> |
| 729 |
<p>You can mark a property as read-only after construction by providing |
| 730 |
a custom <code>setter</code>.</p> |
| 731 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>Person <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Person"</span>, <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 732 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a> <span class="at">birth_date =</span> <span class="fu">new_property</span>(</span> |
| 733 |
<span id="cb19-3"><a href="#cb19-3" tabindex="-1"></a> class_Date,</span> |
| 734 |
<span id="cb19-4"><a href="#cb19-4" tabindex="-1"></a> <span class="at">setter =</span> <span class="cf">function</span>(self, value) {</span> |
| 735 |
<span id="cb19-5"><a href="#cb19-5" tabindex="-1"></a> <span class="cf">if</span>(<span class="sc">!</span><span class="fu">is.null</span>(self<span class="sc">@</span>birth_date)) {</span> |
| 736 |
<span id="cb19-6"><a href="#cb19-6" tabindex="-1"></a> <span class="fu">stop</span>(<span class="st">"@birth_date is read-only"</span>, <span class="at">call. =</span> <span class="cn">FALSE</span>)</span> |
| 737 |
<span id="cb19-7"><a href="#cb19-7" tabindex="-1"></a> }</span> |
| 738 |
<span id="cb19-8"><a href="#cb19-8" tabindex="-1"></a> self<span class="sc">@</span>birth_date <span class="ot"><-</span> <span class="fu">as.Date</span>(value)</span> |
| 739 |
<span id="cb19-9"><a href="#cb19-9" tabindex="-1"></a> self</span> |
| 740 |
<span id="cb19-10"><a href="#cb19-10" tabindex="-1"></a> }</span> |
| 741 |
<span id="cb19-11"><a href="#cb19-11" tabindex="-1"></a>)))</span> |
| 742 |
<span id="cb19-12"><a href="#cb19-12" tabindex="-1"></a></span> |
| 743 |
<span id="cb19-13"><a href="#cb19-13" tabindex="-1"></a>person <span class="ot"><-</span> <span class="fu">Person</span>(<span class="st">"1999-12-31"</span>)</span> |
| 744 |
<span id="cb19-14"><a href="#cb19-14" tabindex="-1"></a></span> |
| 745 |
<span id="cb19-15"><a href="#cb19-15" tabindex="-1"></a><span class="fu">try</span>(person<span class="sc">@</span>birth_date <span class="ot"><-</span> <span class="st">"2000-01-01"</span>)</span> |
| 746 |
<span id="cb19-16"><a href="#cb19-16" tabindex="-1"></a><span class="co">#> Error : @birth_date is read-only</span></span></code></pre></div> |
| 747 |
</div> |
| 748 |
</div> |
| 749 |
</div> |
| 750 |
<div id="constructors" class="section level2"> |
| 751 |
<h2>Constructors</h2> |
| 752 |
<p>You can see the source code for a class’s constructor by accessing |
| 753 |
the <code>constructor</code> property:</p> |
| 754 |
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>Range<span class="sc">@</span>constructor</span> |
| 755 |
<span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a><span class="co">#> function (start = numeric(0), end = numeric(0), length = numeric(0)) </span></span> |
| 756 |
<span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a><span class="co">#> {</span></span> |
| 757 |
<span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a><span class="co">#> start</span></span> |
| 758 |
<span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a><span class="co">#> end</span></span> |
| 759 |
<span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a><span class="co">#> length</span></span> |
| 760 |
<span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a><span class="co">#> S7::new_object(S7::S7_object(), start = start, end = end, </span></span> |
| 761 |
<span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a><span class="co">#> length = length)</span></span> |
| 762 |
<span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a><span class="co">#> }</span></span></code></pre></div> |
| 763 |
<p>In most cases, S7’s default constructor will be all you need. |
| 764 |
However, in some cases you might want something custom. For example, for |
| 765 |
our range class, maybe we’d like to construct it from a vector of |
| 766 |
numeric values, automatically computing the min and the max. To |
| 767 |
implement this we could do:</p> |
| 768 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>Range <span class="ot"><-</span> <span class="fu">new_class</span>(<span class="st">"Range"</span>,</span> |
| 769 |
<span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a> <span class="at">properties =</span> <span class="fu">list</span>(</span> |
| 770 |
<span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a> <span class="at">start =</span> class_numeric,</span> |
| 771 |
<span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a> <span class="at">end =</span> class_numeric</span> |
| 772 |
<span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a> ),</span> |
| 773 |
<span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a> <span class="at">constructor =</span> <span class="cf">function</span>(x) {</span> |
| 774 |
<span id="cb21-7"><a href="#cb21-7" tabindex="-1"></a> <span class="fu">new_object</span>(<span class="fu">S7_object</span>(),</span> |
| 775 |
<span id="cb21-8"><a href="#cb21-8" tabindex="-1"></a> <span class="at">start =</span> <span class="fu">min</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span> |
| 776 |
<span id="cb21-9"><a href="#cb21-9" tabindex="-1"></a> <span class="at">end =</span> <span class="fu">max</span>(x, <span class="at">na.rm =</span> <span class="cn">TRUE</span>))</span> |
| 777 |
<span id="cb21-10"><a href="#cb21-10" tabindex="-1"></a> }</span> |
| 778 |
<span id="cb21-11"><a href="#cb21-11" tabindex="-1"></a>)</span> |
| 779 |
<span id="cb21-12"><a href="#cb21-12" tabindex="-1"></a></span> |
| 780 |
<span id="cb21-13"><a href="#cb21-13" tabindex="-1"></a><span class="fu">Range</span>(<span class="fu">c</span>(<span class="dv">10</span>, <span class="dv">5</span>, <span class="dv">0</span>, <span class="dv">2</span>, <span class="dv">5</span>, <span class="dv">7</span>))</span> |
| 781 |
<span id="cb21-14"><a href="#cb21-14" tabindex="-1"></a><span class="co">#> <Range></span></span> |
| 782 |
<span id="cb21-15"><a href="#cb21-15" tabindex="-1"></a><span class="co">#> @ start: num 0</span></span> |
| 783 |
<span id="cb21-16"><a href="#cb21-16" tabindex="-1"></a><span class="co">#> @ end : num 10</span></span></code></pre></div> |
| 784 |
<p>A constructor must always end with a call to |
| 785 |
<code>new_object()</code>. The first argument to |
| 786 |
<code>new_object()</code> should be an object of the <code>parent</code> |
| 787 |
class (if you haven’t specified a <code>parent</code> argument to |
| 788 |
<code>new_class()</code>, then you should use <code>S7_object()</code> |
| 789 |
as the parent here). That argument should be followed by one named |
| 790 |
argument for each property.</p> |
| 791 |
<p>There’s one drawback of custom constructors that you should be aware |
| 792 |
of: any subclass will also require a custom constructor.</p> |
| 793 |
</div> |
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
<script> |
| 802 |
(function () { |
| 803 |
var script = document.createElement("script"); |
| 804 |
script.type = "text/javascript"; |
| 805 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 806 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 807 |
})(); |
| 808 |
</script> |
| 809 |
|
| 810 |
</body> |
| 811 |
</html> |