-
Notifications
You must be signed in to change notification settings - Fork 48
/
math-evaluator.html
469 lines (429 loc) · 30.5 KB
/
math-evaluator.html
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 17 Math Evaluator | JavaScript for R</title>
<meta name="description" content="Invite JavaScript into your Data Science workflow." />
<meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 17 Math Evaluator | JavaScript for R" />
<meta property="og:type" content="book" />
<meta property="og:description" content="Invite JavaScript into your Data Science workflow." />
<meta name="github-repo" content="JohnCoene/javascript-for-r" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Chapter 17 Math Evaluator | JavaScript for R" />
<meta name="twitter:description" content="Invite JavaScript into your Data Science workflow." />
<meta name="author" content="John Coene" />
<meta name="date" content="2020-07-10" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="prev" href="progress-bar.html"/>
<link rel="next" href="machine-learning.html"/>
<script src="libs/header-attrs/header-attrs.js"></script>
<script src="libs/jquery/jquery.min.js"></script>
<link href="libs/gitbook/css/style.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-clipboard.css" rel="stylesheet" />
<script src="libs/accessible-code-block/empty-anchor.js"></script>
<script src="libs/htmlwidgets/htmlwidgets.js"></script>
<script src="libs/plotly-binding/plotly.js"></script>
<script src="libs/typedarray/typedarray.min.js"></script>
<link href="libs/crosstalk/css/crosstalk.css" rel="stylesheet" />
<script src="libs/crosstalk/js/crosstalk.min.js"></script>
<link href="libs/plotly-htmlwidgets-css/plotly-htmlwidgets.css" rel="stylesheet" />
<script src="libs/plotly-main/plotly-latest.min.js"></script>
<script src="libs/core-js/shim.min.js"></script>
<script src="libs/react/react.min.js"></script>
<script src="libs/react/react-dom.min.js"></script>
<script src="libs/reactwidget/react-tools.js"></script>
<script src="libs/reactable-binding/reactable.js"></script>
<script src="libs/r2d3-render/r2d3-render.js"></script>
<script src="libs/webcomponents/webcomponents.js"></script>
<script src="libs/r2d3-binding/r2d3.js"></script>
<script src="libs/d3v5/d3.min.js"></script>
<style type="text/css">
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
color: #aaaaaa;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
div.sourceCode
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>
<link rel="stylesheet" href="css/style.css" type="text/css" />
<link rel="stylesheet" href="css/toc.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><a href="./">JavaScript for R</a></li>
<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>Preface</a>
<ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#premise"><i class="fa fa-check"></i>Premise</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#book-structure"><i class="fa fa-check"></i>Book Structure</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#disclaimer"><i class="fa fa-check"></i>Disclaimer</a></li>
</ul></li>
<li class="part"><span><b>I Basics & Roadmap</b></span></li>
<li class="chapter" data-level="1" data-path="introduction.html"><a href="introduction.html"><i class="fa fa-check"></i><b>1</b> Introduction</a>
<ul>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#rationale"><i class="fa fa-check"></i>Rationale</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#basics"><i class="fa fa-check"></i>Basics</a>
<ul>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#package-development"><i class="fa fa-check"></i>Package Development</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#json"><i class="fa fa-check"></i>JSON</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#javascript"><i class="fa fa-check"></i>JavaScript</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#methods"><i class="fa fa-check"></i>Methods</a>
<ul>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#v8"><i class="fa fa-check"></i>V8</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#htmlwidgets"><i class="fa fa-check"></i>htmlwidgets</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#shiny"><i class="fa fa-check"></i>Shiny</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#bubble"><i class="fa fa-check"></i>bubble</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#methods-amiss"><i class="fa fa-check"></i>Methods Amiss</a>
<ul>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#reactr"><i class="fa fa-check"></i>reactR</a></li>
<li class="chapter" data-level="" data-path="introduction.html"><a href="introduction.html#r2d3"><i class="fa fa-check"></i>r2d3</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>II JavaScript for Computations</b></span></li>
<li class="chapter" data-level="2" data-path="the-v8-engine.html"><a href="the-v8-engine.html"><i class="fa fa-check"></i><b>2</b> The V8 Engine</a>
<ul>
<li class="chapter" data-level="" data-path="the-v8-engine.html"><a href="the-v8-engine.html#installation"><i class="fa fa-check"></i>Installation</a></li>
<li class="chapter" data-level="" data-path="the-v8-engine.html"><a href="the-v8-engine.html#basics-1"><i class="fa fa-check"></i>Basics</a></li>
<li class="chapter" data-level="" data-path="the-v8-engine.html"><a href="the-v8-engine.html#external-libraries"><i class="fa fa-check"></i>External Libraries</a></li>
<li class="chapter" data-level="" data-path="the-v8-engine.html"><a href="the-v8-engine.html#with-npm"><i class="fa fa-check"></i>With Npm</a></li>
<li class="chapter" data-level="" data-path="the-v8-engine.html"><a href="the-v8-engine.html#use-in-packages"><i class="fa fa-check"></i>Use in Packages</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="node.html"><a href="node.html"><i class="fa fa-check"></i><b>3</b> Node.js with Bubble</a>
<ul>
<li class="chapter" data-level="" data-path="node.html"><a href="node.html#basics-2"><i class="fa fa-check"></i>Basics</a></li>
<li class="chapter" data-level="" data-path="node.html"><a href="node.html#r-markdown-engine"><i class="fa fa-check"></i>R Markdown Engine</a></li>
<li class="chapter" data-level="" data-path="node.html"><a href="node.html#npm"><i class="fa fa-check"></i>Npm</a></li>
</ul></li>
<li class="part"><span><b>III Web Development with Shiny</b></span></li>
<li class="chapter" data-level="4" data-path="introduction-1.html"><a href="introduction-1.html"><i class="fa fa-check"></i><b>4</b> Introduction</a>
<ul>
<li class="chapter" data-level="" data-path="introduction-1.html"><a href="introduction-1.html#static-files"><i class="fa fa-check"></i>Static Files</a></li>
<li class="chapter" data-level="" data-path="introduction-1.html"><a href="introduction-1.html#example---alerts"><i class="fa fa-check"></i>Example - Alerts</a></li>
<li class="chapter" data-level="" data-path="introduction-1.html"><a href="introduction-1.html#from-r-to-javascript"><i class="fa fa-check"></i>From R to JavaScript</a></li>
<li class="chapter" data-level="" data-path="introduction-1.html"><a href="introduction-1.html#from-javascript-to-r"><i class="fa fa-check"></i>From JavaScript to R</a></li>
<li class="chapter" data-level="" data-path="introduction-1.html"><a href="introduction-1.html#deserialise"><i class="fa fa-check"></i>Deserialise</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="a-complete-integration.html"><a href="a-complete-integration.html"><i class="fa fa-check"></i><b>5</b> A Complete Integration</a>
<ul>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#serialisation"><i class="fa fa-check"></i>Serialisation</a></li>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#events-callbacks"><i class="fa fa-check"></i>Events & Callbacks</a></li>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#as-a-package"><i class="fa fa-check"></i>As a Package</a>
<ul>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#dependencies"><i class="fa fa-check"></i>Dependencies</a></li>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#r-code"><i class="fa fa-check"></i>R Code</a></li>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#javascript-code"><i class="fa fa-check"></i>JavaScript Code</a></li>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#input-handler"><i class="fa fa-check"></i>Input Handler</a></li>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#wrapping-up"><i class="fa fa-check"></i>Wrapping up</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="a-complete-integration.html"><a href="a-complete-integration.html#exercises"><i class="fa fa-check"></i>Exercises</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="tips-tricks.html"><a href="tips-tricks.html"><i class="fa fa-check"></i><b>6</b> Tips & Tricks</a>
<ul>
<li class="chapter" data-level="" data-path="tips-tricks.html"><a href="tips-tricks.html#events"><i class="fa fa-check"></i>Events</a></li>
<li class="chapter" data-level="" data-path="tips-tricks.html"><a href="tips-tricks.html#table-buttons"><i class="fa fa-check"></i>Table Buttons</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="custom-outputs.html"><a href="custom-outputs.html"><i class="fa fa-check"></i><b>7</b> Custom Outputs</a>
<ul>
<li class="chapter" data-level="" data-path="custom-outputs.html"><a href="custom-outputs.html#inner-workings"><i class="fa fa-check"></i>Inner-workings</a></li>
<li class="chapter" data-level="" data-path="custom-outputs.html"><a href="custom-outputs.html#discover-lena.js"><i class="fa fa-check"></i>Discover Lena.js</a></li>
<li class="chapter" data-level="" data-path="custom-outputs.html"><a href="custom-outputs.html#setup"><i class="fa fa-check"></i>Setup</a></li>
<li class="chapter" data-level="" data-path="custom-outputs.html"><a href="custom-outputs.html#output"><i class="fa fa-check"></i>Output</a></li>
<li class="chapter" data-level="" data-path="custom-outputs.html"><a href="custom-outputs.html#render"><i class="fa fa-check"></i>Render</a></li>
<li class="chapter" data-level="" data-path="custom-outputs.html"><a href="custom-outputs.html#javascript-binding"><i class="fa fa-check"></i>JavaScript Binding</a></li>
<li class="chapter" data-level="" data-path="custom-outputs.html"><a href="custom-outputs.html#test"><i class="fa fa-check"></i>Test</a></li>
</ul></li>
<li class="part"><span><b>IV Data Visualisation</b></span></li>
<li class="chapter" data-level="8" data-path="introduction-2.html"><a href="introduction-2.html"><i class="fa fa-check"></i><b>8</b> Introduction</a>
<ul>
<li class="chapter" data-level="" data-path="introduction-2.html"><a href="introduction-2.html#candidate-libraries"><i class="fa fa-check"></i>Candidate Libraries</a>
<ul>
<li class="chapter" data-level="" data-path="introduction-2.html"><a href="introduction-2.html#plotly"><i class="fa fa-check"></i>Plotly</a></li>
<li class="chapter" data-level="" data-path="introduction-2.html"><a href="introduction-2.html#highchart.js"><i class="fa fa-check"></i>Highchart.js</a></li>
<li class="chapter" data-level="" data-path="introduction-2.html"><a href="introduction-2.html#chart.js"><i class="fa fa-check"></i>Chart.js</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="introduction-2.html"><a href="introduction-2.html#how-it-works"><i class="fa fa-check"></i>How it works</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="your-first-widget.html"><a href="your-first-widget.html"><i class="fa fa-check"></i><b>9</b> Your First Widget</a>
<ul>
<li class="chapter" data-level="" data-path="your-first-widget.html"><a href="your-first-widget.html#the-scaffold"><i class="fa fa-check"></i>The Scaffold</a></li>
<li class="chapter" data-level="" data-path="your-first-widget.html"><a href="your-first-widget.html#the-output"><i class="fa fa-check"></i>The Output</a></li>
<li class="chapter" data-level="" data-path="your-first-widget.html"><a href="your-first-widget.html#javascript-side"><i class="fa fa-check"></i>JavaScript-side</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html"><i class="fa fa-check"></i><b>10</b> A Realistic Widget</a>
<ul>
<li class="chapter" data-level="" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html#dependency"><i class="fa fa-check"></i>Dependency</a></li>
<li class="chapter" data-level="" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html#javascript-1"><i class="fa fa-check"></i>JavaScript</a></li>
<li class="chapter" data-level="" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html#html-element"><i class="fa fa-check"></i>HTML Element</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="the-full-monty.html"><a href="the-full-monty.html"><i class="fa fa-check"></i><b>11</b> The Full Monty</a>
<ul>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#dependencies-1"><i class="fa fa-check"></i>Dependencies</a></li>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#javascript-2"><i class="fa fa-check"></i>JavaScript</a></li>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#working-with-data"><i class="fa fa-check"></i>Working with Data</a></li>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#transforming-data"><i class="fa fa-check"></i>Transforming Data</a>
<ul>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#in-javascript"><i class="fa fa-check"></i>In JavaScript</a></li>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#in-r"><i class="fa fa-check"></i>In R</a></li>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#pros-cons"><i class="fa fa-check"></i>Pros & Cons</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="the-full-monty.html"><a href="the-full-monty.html#on-print"><i class="fa fa-check"></i>On Print</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="advanced-topics.html"><a href="advanced-topics.html"><i class="fa fa-check"></i><b>12</b> Advanced Topics</a>
<ul>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#shared-variables"><i class="fa fa-check"></i>Shared Variables</a>
<ul>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#sizing"><i class="fa fa-check"></i>Sizing</a></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#sizing-policy"><i class="fa fa-check"></i>Sizing Policy</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#resizing"><i class="fa fa-check"></i>Resizing</a></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#pre-render-hooks-security"><i class="fa fa-check"></i>Pre Render Hooks & Security</a></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#javascript-code-1"><i class="fa fa-check"></i>JavaScript Code</a></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#prepend-append-content"><i class="fa fa-check"></i>Prepend & Append Content</a></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#dependencies-2"><i class="fa fa-check"></i>Dependencies</a></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#compatibility"><i class="fa fa-check"></i>Compatibility</a></li>
<li class="chapter" data-level="" data-path="advanced-topics.html"><a href="advanced-topics.html#unit-tests"><i class="fa fa-check"></i>Unit Tests</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="working-with-shiny.html"><a href="working-with-shiny.html"><i class="fa fa-check"></i><b>13</b> Working with Shiny</a>
<ul>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#javascript-to-r"><i class="fa fa-check"></i>JavaScript to R</a></li>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#input-handler-1"><i class="fa fa-check"></i>Input Handler</a></li>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#r-to-javascript"><i class="fa fa-check"></i>R to JavaScript</a>
<ul>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#send-data"><i class="fa fa-check"></i>Send Data</a></li>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#retrieve-widget-instance"><i class="fa fa-check"></i>Retrieve Widget Instance</a></li>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#handle-data"><i class="fa fa-check"></i>Handle Data</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#proxy-function"><i class="fa fa-check"></i>Proxy Function</a></li>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#clear-data"><i class="fa fa-check"></i>Clear Data</a></li>
<li class="chapter" data-level="" data-path="working-with-shiny.html"><a href="working-with-shiny.html#update"><i class="fa fa-check"></i>Update</a></li>
</ul></li>
<li class="part"><span><b>V Examples</b></span></li>
<li class="chapter" data-level="14" data-path="ploty.html"><a href="ploty.html"><i class="fa fa-check"></i><b>14</b> Ploty</a>
<ul>
<li class="chapter" data-level="" data-path="ploty.html"><a href="ploty.html#discover"><i class="fa fa-check"></i>Discover</a></li>
<li class="chapter" data-level="" data-path="ploty.html"><a href="ploty.html#basics-3"><i class="fa fa-check"></i>Basics</a></li>
<li class="chapter" data-level="" data-path="ploty.html"><a href="ploty.html#javascript-to-r-1"><i class="fa fa-check"></i>JavaScript to R</a></li>
<li class="chapter" data-level="" data-path="ploty.html"><a href="ploty.html#r-to-javascript-1"><i class="fa fa-check"></i>R to JavaScript</a></li>
<li class="chapter" data-level="" data-path="ploty.html"><a href="ploty.html#size-resize"><i class="fa fa-check"></i>Size & Resize</a></li>
<li class="chapter" data-level="" data-path="ploty.html"><a href="ploty.html#performance-security"><i class="fa fa-check"></i>Performance & Security</a></li>
</ul></li>
<li class="chapter" data-level="15" data-path="image-classifier.html"><a href="image-classifier.html"><i class="fa fa-check"></i><b>15</b> Image Classifier</a>
<ul>
<li class="chapter" data-level="" data-path="image-classifier.html"><a href="image-classifier.html#discover-1"><i class="fa fa-check"></i>Discover</a></li>
<li class="chapter" data-level="" data-path="image-classifier.html"><a href="image-classifier.html#setup-1"><i class="fa fa-check"></i>Setup</a></li>
<li class="chapter" data-level="" data-path="image-classifier.html"><a href="image-classifier.html#javascript-3"><i class="fa fa-check"></i>JavaScript</a></li>
<li class="chapter" data-level="" data-path="image-classifier.html"><a href="image-classifier.html#input-handler-2"><i class="fa fa-check"></i>Input Handler</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="progress-bar.html"><a href="progress-bar.html"><i class="fa fa-check"></i><b>16</b> Progress Bar</a>
<ul>
<li class="chapter" data-level="" data-path="progress-bar.html"><a href="progress-bar.html#r-code-1"><i class="fa fa-check"></i>R code</a></li>
<li class="chapter" data-level="" data-path="progress-bar.html"><a href="progress-bar.html#javascript-code-2"><i class="fa fa-check"></i>JavaScript code</a></li>
<li class="chapter" data-level="" data-path="progress-bar.html"><a href="progress-bar.html#events-1"><i class="fa fa-check"></i>Events</a></li>
</ul></li>
<li class="chapter" data-level="17" data-path="math-evaluator.html"><a href="math-evaluator.html"><i class="fa fa-check"></i><b>17</b> Math Evaluator</a></li>
<li class="chapter" data-level="18" data-path="machine-learning.html"><a href="machine-learning.html"><i class="fa fa-check"></i><b>18</b> Machine Learning</a>
<ul>
<li class="chapter" data-level="" data-path="machine-learning.html"><a href="machine-learning.html#dependency-1"><i class="fa fa-check"></i>Dependency</a></li>
<li class="chapter" data-level="" data-path="machine-learning.html"><a href="machine-learning.html#simple-regression"><i class="fa fa-check"></i>Simple Regression</a></li>
</ul></li>
<li class="chapter" data-level="19" data-path="cookies.html"><a href="cookies.html"><i class="fa fa-check"></i><b>19</b> Cookies</a>
<ul>
<li class="chapter" data-level="" data-path="cookies.html"><a href="cookies.html#discover-js-cookie"><i class="fa fa-check"></i>Discover js-cookie</a></li>
<li class="chapter" data-level="" data-path="cookies.html"><a href="cookies.html#setup-2"><i class="fa fa-check"></i>Setup</a></li>
<li class="chapter" data-level="" data-path="cookies.html"><a href="cookies.html#javascript-4"><i class="fa fa-check"></i>JavaScript</a></li>
<li class="chapter" data-level="" data-path="cookies.html"><a href="cookies.html#r-code-2"><i class="fa fa-check"></i>R Code</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>References</a></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">JavaScript for R</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="math-evaluator" class="section level1" number="17">
<h1><span class="header-section-number">Chapter 17</span> Math Evaluator</h1>
<p>In this short example we wrap the JavaScript library <a href="https://github.com/5anthosh/fcal">fcal.js</a> with R using V8.</p>
<blockquote>
<p>Fcaljs is an extensive math expression evaluator library for JavaScript and Node.js. Using fcal, you can perform basic arithmetic, percentage operations with precision. It features a flexible expression parser with a large set of built-in units, functions and constants.</p>
</blockquote>
<p>The library can be explored in its <a href="https://github.com/5anthosh/fcal">README</a> available on Github, the library is very straightforward it consists of instantiating a class and use the <code>evaluate</code> method to evaluate mathematical expressions.</p>
<p>We start by loading V8 and sourcing fcal.js from the CDN.</p>
<div class="sourceCode" id="cb389"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb389-1"><a href="math-evaluator.html#cb389-1"></a><span class="kw">library</span>(V8)</span>
<span id="cb389-2"><a href="math-evaluator.html#cb389-2"></a></span>
<span id="cb389-3"><a href="math-evaluator.html#cb389-3"></a><span class="co"># initialise</span></span>
<span id="cb389-4"><a href="math-evaluator.html#cb389-4"></a>engine <-<span class="st"> </span><span class="kw">v8</span>()</span>
<span id="cb389-5"><a href="math-evaluator.html#cb389-5"></a></span>
<span id="cb389-6"><a href="math-evaluator.html#cb389-6"></a>engine<span class="op">$</span><span class="kw">source</span>(<span class="st">"https://cdn.jsdelivr.net/npm/fcal/dist/fcal.js"</span>)</span></code></pre></div>
<p>Then one needs to initialise the class.</p>
<div class="sourceCode" id="cb390"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb390-1"><a href="math-evaluator.html#cb390-1"></a>engine<span class="op">$</span><span class="kw">eval</span>(<span class="st">"const calc = new fcal.Fcal();"</span>)</span></code></pre></div>
<p>Finally, it’s simply about using the one method to evaluate expressions.</p>
<div class="sourceCode" id="cb391"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb391-1"><a href="math-evaluator.html#cb391-1"></a>engine<span class="op">$</span><span class="kw">call</span>(<span class="st">"calc.evaluate"</span>, <span class="st">"23 C add 123 F"</span>)</span></code></pre></div>
<pre><code>## $n
## [1] "196.40000000000004"
##
## $ns
## $ns$name
## [1] "Decimal"
##
##
## $lf
## [1] FALSE
##
## $unit
## $unit$id
## [1] "TEMPERATURE"
##
## $unit$r
## [1] "0.55555555555555555556"
##
## $unit$b
## [1] "255.3722222222222"
##
## $unit$unitType
## [1] "°F"
##
## $unit$plural
## [1] "°F"
##
## $unit$singular
## [1] "°F"
##
##
## $TYPE
## [1] 1
##
## $TYPE_RANK
## [1] 2</code></pre>
</div>
</section>
</div>
</div>
</div>
<a href="progress-bar.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="machine-learning.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook/js/app.min.js"></script>
<script src="libs/gitbook/js/lunr.js"></script>
<script src="libs/gitbook/js/clipboard.min.js"></script>
<script src="libs/gitbook/js/plugin-search.js"></script>
<script src="libs/gitbook/js/plugin-sharing.js"></script>
<script src="libs/gitbook/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook/js/plugin-bookdown.js"></script>
<script src="libs/gitbook/js/jquery.highlight.js"></script>
<script src="libs/gitbook/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": false,
"twitter": false,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": {}
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/JohnCoene/javascript-for-r/edit/master/33-examples-fcal.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": {},
"toc": {
"collapse": "section"
}
});
});
</script>
</body>
</html>