-
Notifications
You must be signed in to change notification settings - Fork 48
/
ploty.html
786 lines (743 loc) Β· 75.6 KB
/
ploty.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
<!DOCTYPE html>
<html lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>Chapter 11 Ploty | JavaScript for R</title>
<meta name="description" content="Invite JavaScript into your Data Science workflow." />
<meta name="generator" content="bookdown 0.20 and GitBook 2.6.7" />
<meta property="og:title" content="Chapter 11 Ploty | 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 11 Ploty | 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-08-22" />
<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="wrap-up.html"/>
<link rel="next" href="discover.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>
<script src="libs/viz/viz.js"></script>
<link href="libs/DiagrammeR-styles/styles.css" rel="stylesheet" />
<script src="libs/grViz-binding/grViz.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#acknowledgement"><i class="fa fa-check"></i>Acknowledgement</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="1.1" data-path="introduction.html"><a href="introduction.html#rationale"><i class="fa fa-check"></i><b>1.1</b> Rationale</a></li>
<li class="chapter" data-level="1.2" data-path="introduction.html"><a href="introduction.html#methods"><i class="fa fa-check"></i><b>1.2</b> Methods</a>
<ul>
<li class="chapter" data-level="1.2.1" data-path="introduction.html"><a href="introduction.html#v8"><i class="fa fa-check"></i><b>1.2.1</b> V8</a></li>
<li class="chapter" data-level="1.2.2" data-path="introduction.html"><a href="introduction.html#htmlwidgets"><i class="fa fa-check"></i><b>1.2.2</b> htmlwidgets</a></li>
<li class="chapter" data-level="1.2.3" data-path="introduction.html"><a href="introduction.html#shiny"><i class="fa fa-check"></i><b>1.2.3</b> Shiny</a></li>
<li class="chapter" data-level="1.2.4" data-path="introduction.html"><a href="introduction.html#bubble"><i class="fa fa-check"></i><b>1.2.4</b> bubble</a></li>
</ul></li>
<li class="chapter" data-level="1.3" data-path="introduction.html"><a href="introduction.html#methods-amiss"><i class="fa fa-check"></i><b>1.3</b> Methods Amiss</a>
<ul>
<li class="chapter" data-level="1.3.1" data-path="introduction.html"><a href="introduction.html#reactr"><i class="fa fa-check"></i><b>1.3.1</b> reactR</a></li>
<li class="chapter" data-level="1.3.2" data-path="introduction.html"><a href="introduction.html#r2d3"><i class="fa fa-check"></i><b>1.3.2</b> r2d3</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="prerequisites.html"><a href="prerequisites.html"><i class="fa fa-check"></i><b>2</b> Prerequisites</a>
<ul>
<li class="chapter" data-level="2.1" data-path="prerequisites.html"><a href="prerequisites.html#r-package-development"><i class="fa fa-check"></i><b>2.1</b> R Package Development</a>
<ul>
<li class="chapter" data-level="2.1.1" data-path="prerequisites.html"><a href="prerequisites.html#creating-a-package"><i class="fa fa-check"></i><b>2.1.1</b> Creating a Package</a></li>
<li class="chapter" data-level="2.1.2" data-path="prerequisites.html"><a href="prerequisites.html#metadata"><i class="fa fa-check"></i><b>2.1.2</b> Metadata</a></li>
<li class="chapter" data-level="2.1.3" data-path="prerequisites.html"><a href="prerequisites.html#r-code"><i class="fa fa-check"></i><b>2.1.3</b> R code</a></li>
<li class="chapter" data-level="2.1.4" data-path="prerequisites.html"><a href="prerequisites.html#documentation"><i class="fa fa-check"></i><b>2.1.4</b> Documentation</a></li>
<li class="chapter" data-level="2.1.5" data-path="prerequisites.html"><a href="prerequisites.html#installed-files"><i class="fa fa-check"></i><b>2.1.5</b> Installed files</a></li>
<li class="chapter" data-level="2.1.6" data-path="prerequisites.html"><a href="prerequisites.html#build-load-and-install"><i class="fa fa-check"></i><b>2.1.6</b> Build, load, and install</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="prerequisites.html"><a href="prerequisites.html#json"><i class="fa fa-check"></i><b>2.2</b> JSON</a>
<ul>
<li class="chapter" data-level="2.2.1" data-path="prerequisites.html"><a href="prerequisites.html#serialising"><i class="fa fa-check"></i><b>2.2.1</b> Serialising</a></li>
<li class="chapter" data-level="2.2.2" data-path="prerequisites.html"><a href="prerequisites.html#tabular-data"><i class="fa fa-check"></i><b>2.2.2</b> Tabular data</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="prerequisites.html"><a href="prerequisites.html#javascript"><i class="fa fa-check"></i><b>2.3</b> JavaScript</a>
<ul>
<li class="chapter" data-level="2.3.1" data-path="prerequisites.html"><a href="prerequisites.html#developer-tools"><i class="fa fa-check"></i><b>2.3.1</b> Developer tools</a></li>
<li class="chapter" data-level="2.3.2" data-path="prerequisites.html"><a href="prerequisites.html#variable-declaration-and-scope"><i class="fa fa-check"></i><b>2.3.2</b> Variable declaration and scope</a></li>
<li class="chapter" data-level="2.3.3" data-path="prerequisites.html"><a href="prerequisites.html#document-object-model"><i class="fa fa-check"></i><b>2.3.3</b> Document object model</a></li>
</ul></li>
</ul></li>
<li class="part"><span><b>II Data Visualisation</b></span></li>
<li class="chapter" data-level="3" data-path="introduction-to-widgets.html"><a href="introduction-to-widgets.html"><i class="fa fa-check"></i><b>3</b> Introduction to widgets</a>
<ul>
<li class="chapter" data-level="3.1" data-path="introduction-to-widgets.html"><a href="introduction-to-widgets.html#plotly-package"><i class="fa fa-check"></i><b>3.1</b> Plotly package</a></li>
<li class="chapter" data-level="3.2" data-path="introduction-to-widgets.html"><a href="introduction-to-widgets.html#dt-package"><i class="fa fa-check"></i><b>3.2</b> DT package</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="basics-of-building-widgets.html"><a href="basics-of-building-widgets.html"><i class="fa fa-check"></i><b>4</b> Basics of building widgets</a>
<ul>
<li class="chapter" data-level="4.1" data-path="basics-of-building-widgets.html"><a href="basics-of-building-widgets.html#candidate-libraries"><i class="fa fa-check"></i><b>4.1</b> Candidate Libraries</a>
<ul>
<li class="chapter" data-level="4.1.1" data-path="basics-of-building-widgets.html"><a href="basics-of-building-widgets.html#plotly"><i class="fa fa-check"></i><b>4.1.1</b> Plotly</a></li>
<li class="chapter" data-level="4.1.2" data-path="basics-of-building-widgets.html"><a href="basics-of-building-widgets.html#highchart.js"><i class="fa fa-check"></i><b>4.1.2</b> Highchart.js</a></li>
<li class="chapter" data-level="4.1.3" data-path="basics-of-building-widgets.html"><a href="basics-of-building-widgets.html#chart.js"><i class="fa fa-check"></i><b>4.1.3</b> Chart.js</a></li>
</ul></li>
<li class="chapter" data-level="4.2" data-path="basics-of-building-widgets.html"><a href="basics-of-building-widgets.html#how-it-works"><i class="fa fa-check"></i><b>4.2</b> How it works</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="your-first-widget.html"><a href="your-first-widget.html"><i class="fa fa-check"></i><b>5</b> Your First Widget</a>
<ul>
<li class="chapter" data-level="5.1" data-path="your-first-widget.html"><a href="your-first-widget.html#the-scaffold"><i class="fa fa-check"></i><b>5.1</b> The Scaffold</a></li>
<li class="chapter" data-level="5.2" data-path="your-first-widget.html"><a href="your-first-widget.html#the-output"><i class="fa fa-check"></i><b>5.2</b> The Output</a></li>
<li class="chapter" data-level="5.3" data-path="your-first-widget.html"><a href="your-first-widget.html#javascript-side"><i class="fa fa-check"></i><b>5.3</b> JavaScript-side</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html"><i class="fa fa-check"></i><b>6</b> A Realistic Widget</a>
<ul>
<li class="chapter" data-level="6.1" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html#dependency"><i class="fa fa-check"></i><b>6.1</b> Dependency</a></li>
<li class="chapter" data-level="6.2" data-path="prerequisites.html"><a href="prerequisites.html#javascript"><i class="fa fa-check"></i><b>6.2</b> JavaScript</a></li>
<li class="chapter" data-level="6.3" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html#html-element"><i class="fa fa-check"></i><b>6.3</b> HTML Element</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="the-full-monty.html"><a href="the-full-monty.html"><i class="fa fa-check"></i><b>7</b> The Full Monty</a>
<ul>
<li class="chapter" data-level="7.1" data-path="the-full-monty.html"><a href="the-full-monty.html#dependencies"><i class="fa fa-check"></i><b>7.1</b> Dependencies</a></li>
<li class="chapter" data-level="7.2" data-path="prerequisites.html"><a href="prerequisites.html#javascript"><i class="fa fa-check"></i><b>7.2</b> JavaScript</a></li>
<li class="chapter" data-level="7.3" data-path="the-full-monty.html"><a href="the-full-monty.html#working-with-data"><i class="fa fa-check"></i><b>7.3</b> Working with Data</a></li>
<li class="chapter" data-level="7.4" data-path="the-full-monty.html"><a href="the-full-monty.html#transforming-data"><i class="fa fa-check"></i><b>7.4</b> Transforming Data</a>
<ul>
<li class="chapter" data-level="7.4.1" data-path="the-full-monty.html"><a href="the-full-monty.html#in-javascript"><i class="fa fa-check"></i><b>7.4.1</b> In JavaScript</a></li>
<li class="chapter" data-level="7.4.2" data-path="the-full-monty.html"><a href="the-full-monty.html#in-r"><i class="fa fa-check"></i><b>7.4.2</b> In R</a></li>
<li class="chapter" data-level="7.4.3" data-path="the-full-monty.html"><a href="the-full-monty.html#pros-cons"><i class="fa fa-check"></i><b>7.4.3</b> Pros & Cons</a></li>
</ul></li>
<li class="chapter" data-level="7.5" data-path="the-full-monty.html"><a href="the-full-monty.html#on-print"><i class="fa fa-check"></i><b>7.5</b> On Print</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="advanced-topics.html"><a href="advanced-topics.html"><i class="fa fa-check"></i><b>8</b> Advanced Topics</a>
<ul>
<li class="chapter" data-level="8.1" data-path="advanced-topics.html"><a href="advanced-topics.html#shared-variables"><i class="fa fa-check"></i><b>8.1</b> Shared Variables</a>
<ul>
<li class="chapter" data-level="8.1.1" data-path="advanced-topics.html"><a href="advanced-topics.html#sizing"><i class="fa fa-check"></i><b>8.1.1</b> Sizing</a></li>
<li class="chapter" data-level="8.1.2" data-path="advanced-topics.html"><a href="advanced-topics.html#sizing-policy"><i class="fa fa-check"></i><b>8.1.2</b> Sizing Policy</a></li>
</ul></li>
<li class="chapter" data-level="8.2" data-path="advanced-topics.html"><a href="advanced-topics.html#resizing"><i class="fa fa-check"></i><b>8.2</b> Resizing</a></li>
<li class="chapter" data-level="8.3" data-path="advanced-topics.html"><a href="advanced-topics.html#pre-render-hooks-security"><i class="fa fa-check"></i><b>8.3</b> Pre Render Hooks & Security</a></li>
<li class="chapter" data-level="8.4" data-path="advanced-topics.html"><a href="advanced-topics.html#javascript-code"><i class="fa fa-check"></i><b>8.4</b> JavaScript Code</a></li>
<li class="chapter" data-level="8.5" data-path="advanced-topics.html"><a href="advanced-topics.html#prepend-append-content"><i class="fa fa-check"></i><b>8.5</b> Prepend & Append Content</a></li>
<li class="chapter" data-level="8.6" data-path="the-full-monty.html"><a href="the-full-monty.html#dependencies"><i class="fa fa-check"></i><b>8.6</b> Dependencies</a></li>
<li class="chapter" data-level="8.7" data-path="advanced-topics.html"><a href="advanced-topics.html#compatibility"><i class="fa fa-check"></i><b>8.7</b> Compatibility</a></li>
<li class="chapter" data-level="8.8" data-path="advanced-topics.html"><a href="advanced-topics.html#unit-tests"><i class="fa fa-check"></i><b>8.8</b> Unit Tests</a></li>
<li class="chapter" data-level="8.9" data-path="advanced-topics.html"><a href="advanced-topics.html#performances"><i class="fa fa-check"></i><b>8.9</b> Performances</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="crosstalk.html"><a href="crosstalk.html"><i class="fa fa-check"></i><b>9</b> Crosstalk</a>
<ul>
<li class="chapter" data-level="9.1" data-path="crosstalk.html"><a href="crosstalk.html#crosstalk-example"><i class="fa fa-check"></i><b>9.1</b> Crosstalk example</a></li>
<li class="chapter" data-level="9.2" data-path="crosstalk.html"><a href="crosstalk.html#crosstalk-requirements"><i class="fa fa-check"></i><b>9.2</b> Crosstalk requirements</a></li>
<li class="chapter" data-level="9.3" data-path="basics-of-building-widgets.html"><a href="basics-of-building-widgets.html#how-it-works"><i class="fa fa-check"></i><b>9.3</b> How it works</a></li>
<li class="chapter" data-level="9.4" data-path="crosstalk.html"><a href="crosstalk.html#crosstalk-with-gio"><i class="fa fa-check"></i><b>9.4</b> Crosstalk with gio</a></li>
<li class="chapter" data-level="9.5" data-path="crosstalk.html"><a href="crosstalk.html#adapt-the-r-code"><i class="fa fa-check"></i><b>9.5</b> Adapt the R code</a></li>
<li class="chapter" data-level="9.6" data-path="crosstalk.html"><a href="crosstalk.html#change-the-javascript-code"><i class="fa fa-check"></i><b>9.6</b> Change the JavaScript code</a>
<ul>
<li class="chapter" data-level="9.6.1" data-path="crosstalk.html"><a href="crosstalk.html#send-selected-keys"><i class="fa fa-check"></i><b>9.6.1</b> Send selected keys</a></li>
<li class="chapter" data-level="9.6.2" data-path="crosstalk.html"><a href="crosstalk.html#set-selected-keys"><i class="fa fa-check"></i><b>9.6.2</b> Set selected keys</a></li>
<li class="chapter" data-level="9.6.3" data-path="crosstalk.html"><a href="crosstalk.html#using-crosstalk-with-gio"><i class="fa fa-check"></i><b>9.6.3</b> Using crosstalk with gio</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="10" data-path="wrap-up.html"><a href="wrap-up.html"><i class="fa fa-check"></i><b>10</b> Wrap up</a>
<ul>
<li class="chapter" data-level="10.1" data-path="the-full-monty.html"><a href="the-full-monty.html#working-with-data"><i class="fa fa-check"></i><b>10.1</b> Working with data</a></li>
<li class="chapter" data-level="10.2" data-path="wrap-up.html"><a href="wrap-up.html#plethora-of-options"><i class="fa fa-check"></i><b>10.2</b> Plethora of options</a></li>
<li class="chapter" data-level="10.3" data-path="wrap-up.html"><a href="wrap-up.html#interface-design"><i class="fa fa-check"></i><b>10.3</b> Interface design</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="ploty.html"><a href="ploty.html"><i class="fa fa-check"></i><b>11</b> Ploty</a>
<ul>
<li class="chapter" data-level="11.1" data-path="ploty.html"><a href="ploty.html#discover"><i class="fa fa-check"></i><b>11.1</b> Discover</a></li>
<li class="chapter" data-level="11.2" data-path="ploty.html"><a href="ploty.html#basics"><i class="fa fa-check"></i><b>11.2</b> Basics</a></li>
<li class="chapter" data-level="11.3" data-path="ploty.html"><a href="ploty.html#javascript-to-r"><i class="fa fa-check"></i><b>11.3</b> JavaScript to R</a></li>
<li class="chapter" data-level="11.4" data-path="ploty.html"><a href="ploty.html#r-to-javascript"><i class="fa fa-check"></i><b>11.4</b> R to JavaScript</a></li>
<li class="chapter" data-level="11.5" data-path="ploty.html"><a href="ploty.html#size-resize"><i class="fa fa-check"></i><b>11.5</b> Size & Resize</a></li>
<li class="chapter" data-level="11.6" data-path="ploty.html"><a href="ploty.html#performance-security"><i class="fa fa-check"></i><b>11.6</b> Performance & Security</a></li>
</ul></li>
<li class="part"><span><b>III Web Development with Shiny</b></span></li>
<li class="chapter" data-level="12" data-path="ploty.html"><a href="ploty.html#discover"><i class="fa fa-check"></i><b>12</b> Discover</a>
<ul>
<li class="chapter" data-level="12.1" data-path="discover.html"><a href="discover.html"><i class="fa fa-check"></i><b>12.1</b> Static Files</a></li>
<li class="chapter" data-level="12.2" data-path="discover.html"><a href="discover.html#example---alerts"><i class="fa fa-check"></i><b>12.2</b> Example - Alerts</a></li>
<li class="chapter" data-level="12.3" data-path="discover.html"><a href="discover.html#from-r-to-javascript"><i class="fa fa-check"></i><b>12.3</b> From R to JavaScript</a></li>
<li class="chapter" data-level="12.4" data-path="discover.html"><a href="discover.html#from-javascript-to-r"><i class="fa fa-check"></i><b>12.4</b> From JavaScript to R</a></li>
<li class="chapter" data-level="12.5" data-path="discover.html"><a href="discover.html#deserialise"><i class="fa fa-check"></i><b>12.5</b> Deserialise</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="a-complete-integration.html"><a href="a-complete-integration.html"><i class="fa fa-check"></i><b>13</b> A Complete Integration</a>
<ul>
<li class="chapter" data-level="13.1" data-path="a-complete-integration.html"><a href="a-complete-integration.html#serialisation"><i class="fa fa-check"></i><b>13.1</b> Serialisation</a></li>
<li class="chapter" data-level="13.2" data-path="a-complete-integration.html"><a href="a-complete-integration.html#events-callbacks"><i class="fa fa-check"></i><b>13.2</b> Events & Callbacks</a></li>
<li class="chapter" data-level="13.3" data-path="a-complete-integration.html"><a href="a-complete-integration.html#as-a-package"><i class="fa fa-check"></i><b>13.3</b> As a Package</a>
<ul>
<li class="chapter" data-level="13.3.1" data-path="the-full-monty.html"><a href="the-full-monty.html#dependencies"><i class="fa fa-check"></i><b>13.3.1</b> Dependencies</a></li>
<li class="chapter" data-level="13.3.2" data-path="prerequisites.html"><a href="prerequisites.html#r-code"><i class="fa fa-check"></i><b>13.3.2</b> R Code</a></li>
<li class="chapter" data-level="13.3.3" data-path="advanced-topics.html"><a href="advanced-topics.html#javascript-code"><i class="fa fa-check"></i><b>13.3.3</b> JavaScript Code</a></li>
<li class="chapter" data-level="13.3.4" data-path="a-complete-integration.html"><a href="a-complete-integration.html#input-handler"><i class="fa fa-check"></i><b>13.3.4</b> Input Handler</a></li>
<li class="chapter" data-level="13.3.5" data-path="a-complete-integration.html"><a href="a-complete-integration.html#wrapping-up"><i class="fa fa-check"></i><b>13.3.5</b> Wrapping up</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="14" data-path="tips-tricks.html"><a href="tips-tricks.html"><i class="fa fa-check"></i><b>14</b> Tips & Tricks</a>
<ul>
<li class="chapter" data-level="14.1" data-path="tips-tricks.html"><a href="tips-tricks.html#shiny-events"><i class="fa fa-check"></i><b>14.1</b> Shiny Events</a></li>
<li class="chapter" data-level="14.2" data-path="tips-tricks.html"><a href="tips-tricks.html#table-buttons"><i class="fa fa-check"></i><b>14.2</b> Table Buttons</a></li>
<li class="chapter" data-level="14.3" data-path="tips-tricks.html"><a href="tips-tricks.html#jquery---toggle"><i class="fa fa-check"></i><b>14.3</b> jQuery - Toggle</a></li>
</ul></li>
<li class="chapter" data-level="15" data-path="custom-outputs.html"><a href="custom-outputs.html"><i class="fa fa-check"></i><b>15</b> Custom Outputs</a>
<ul>
<li class="chapter" data-level="15.1" data-path="custom-outputs.html"><a href="custom-outputs.html#inner-workings"><i class="fa fa-check"></i><b>15.1</b> Inner-workings</a></li>
<li class="chapter" data-level="15.2" data-path="custom-outputs.html"><a href="custom-outputs.html#discover-lena.js"><i class="fa fa-check"></i><b>15.2</b> Discover Lena.js</a></li>
<li class="chapter" data-level="15.3" data-path="custom-outputs.html"><a href="custom-outputs.html#setup"><i class="fa fa-check"></i><b>15.3</b> Setup</a></li>
<li class="chapter" data-level="15.4" data-path="custom-outputs.html"><a href="custom-outputs.html#output"><i class="fa fa-check"></i><b>15.4</b> Output</a></li>
<li class="chapter" data-level="15.5" data-path="custom-outputs.html"><a href="custom-outputs.html#render"><i class="fa fa-check"></i><b>15.5</b> Render</a></li>
<li class="chapter" data-level="15.6" data-path="custom-outputs.html"><a href="custom-outputs.html#javascript-binding"><i class="fa fa-check"></i><b>15.6</b> JavaScript Binding</a></li>
<li class="chapter" data-level="15.7" data-path="custom-outputs.html"><a href="custom-outputs.html#test"><i class="fa fa-check"></i><b>15.7</b> Test</a></li>
</ul></li>
<li class="chapter" data-level="16" data-path="widgets-with-shiny.html"><a href="widgets-with-shiny.html"><i class="fa fa-check"></i><b>16</b> Widgets with Shiny</a>
<ul>
<li class="chapter" data-level="16.1" data-path="ploty.html"><a href="ploty.html#javascript-to-r"><i class="fa fa-check"></i><b>16.1</b> JavaScript to R</a></li>
<li class="chapter" data-level="16.2" data-path="a-complete-integration.html"><a href="a-complete-integration.html#input-handler"><i class="fa fa-check"></i><b>16.2</b> Input Handler</a></li>
<li class="chapter" data-level="16.3" data-path="ploty.html"><a href="ploty.html#r-to-javascript"><i class="fa fa-check"></i><b>16.3</b> R to JavaScript</a>
<ul>
<li class="chapter" data-level="16.3.1" data-path="widgets-with-shiny.html"><a href="widgets-with-shiny.html#send-data"><i class="fa fa-check"></i><b>16.3.1</b> Send Data</a></li>
<li class="chapter" data-level="16.3.2" data-path="widgets-with-shiny.html"><a href="widgets-with-shiny.html#retrieve-widget-instance"><i class="fa fa-check"></i><b>16.3.2</b> Retrieve Widget Instance</a></li>
<li class="chapter" data-level="16.3.3" data-path="widgets-with-shiny.html"><a href="widgets-with-shiny.html#handle-data"><i class="fa fa-check"></i><b>16.3.3</b> Handle Data</a></li>
</ul></li>
<li class="chapter" data-level="16.4" data-path="widgets-with-shiny.html"><a href="widgets-with-shiny.html#proxy-function"><i class="fa fa-check"></i><b>16.4</b> Proxy Function</a></li>
<li class="chapter" data-level="16.5" data-path="widgets-with-shiny.html"><a href="widgets-with-shiny.html#clear-data"><i class="fa fa-check"></i><b>16.5</b> Clear Data</a></li>
<li class="chapter" data-level="16.6" data-path="widgets-with-shiny.html"><a href="widgets-with-shiny.html#update"><i class="fa fa-check"></i><b>16.6</b> Update</a></li>
</ul></li>
<li class="part"><span><b>IV JavaScript for Computations</b></span></li>
<li class="chapter" data-level="17" data-path="the-v8-engine.html"><a href="the-v8-engine.html"><i class="fa fa-check"></i><b>17</b> The V8 Engine</a>
<ul>
<li class="chapter" data-level="17.1" data-path="the-v8-engine.html"><a href="the-v8-engine.html#installation"><i class="fa fa-check"></i><b>17.1</b> Installation</a></li>
<li class="chapter" data-level="17.2" data-path="ploty.html"><a href="ploty.html#basics"><i class="fa fa-check"></i><b>17.2</b> Basics</a></li>
<li class="chapter" data-level="17.3" data-path="the-v8-engine.html"><a href="the-v8-engine.html#external-libraries"><i class="fa fa-check"></i><b>17.3</b> External Libraries</a></li>
<li class="chapter" data-level="17.4" data-path="the-v8-engine.html"><a href="the-v8-engine.html#with-npm"><i class="fa fa-check"></i><b>17.4</b> With Npm</a></li>
<li class="chapter" data-level="17.5" data-path="the-v8-engine.html"><a href="the-v8-engine.html#use-in-packages"><i class="fa fa-check"></i><b>17.5</b> Use in Packages</a></li>
</ul></li>
<li class="chapter" data-level="18" data-path="node.html"><a href="node.html"><i class="fa fa-check"></i><b>18</b> Node.js with Bubble</a>
<ul>
<li class="chapter" data-level="18.1" data-path="ploty.html"><a href="ploty.html#basics"><i class="fa fa-check"></i><b>18.1</b> Basics</a></li>
<li class="chapter" data-level="18.2" data-path="node.html"><a href="node.html#r-markdown-engine"><i class="fa fa-check"></i><b>18.2</b> R Markdown Engine</a></li>
<li class="chapter" data-level="18.3" data-path="node.html"><a href="node.html#npm"><i class="fa fa-check"></i><b>18.3</b> Npm</a></li>
</ul></li>
<li class="part"><span><b>V Examples</b></span></li>
<li class="chapter" data-level="19" data-path="image-classifier.html"><a href="image-classifier.html"><i class="fa fa-check"></i><b>19</b> Image Classifier</a>
<ul>
<li class="chapter" data-level="19.1" data-path="ploty.html"><a href="ploty.html#discover"><i class="fa fa-check"></i><b>19.1</b> Discover</a></li>
<li class="chapter" data-level="19.2" data-path="custom-outputs.html"><a href="custom-outputs.html#setup"><i class="fa fa-check"></i><b>19.2</b> Setup</a></li>
<li class="chapter" data-level="19.3" data-path="prerequisites.html"><a href="prerequisites.html#javascript"><i class="fa fa-check"></i><b>19.3</b> JavaScript</a></li>
<li class="chapter" data-level="19.4" data-path="a-complete-integration.html"><a href="a-complete-integration.html#input-handler"><i class="fa fa-check"></i><b>19.4</b> Input Handler</a></li>
</ul></li>
<li class="chapter" data-level="20" data-path="progress-bar.html"><a href="progress-bar.html"><i class="fa fa-check"></i><b>20</b> Progress Bar</a>
<ul>
<li class="chapter" data-level="20.1" data-path="prerequisites.html"><a href="prerequisites.html#r-code"><i class="fa fa-check"></i><b>20.1</b> R code</a></li>
<li class="chapter" data-level="20.2" data-path="advanced-topics.html"><a href="advanced-topics.html#javascript-code"><i class="fa fa-check"></i><b>20.2</b> JavaScript code</a></li>
<li class="chapter" data-level="20.3" data-path="progress-bar.html"><a href="progress-bar.html#events"><i class="fa fa-check"></i><b>20.3</b> Events</a></li>
</ul></li>
<li class="chapter" data-level="21" data-path="machine-learning.html"><a href="machine-learning.html"><i class="fa fa-check"></i><b>21</b> Machine Learning</a>
<ul>
<li class="chapter" data-level="21.1" data-path="a-realistic-widget.html"><a href="a-realistic-widget.html#dependency"><i class="fa fa-check"></i><b>21.1</b> Dependency</a></li>
<li class="chapter" data-level="21.2" data-path="machine-learning.html"><a href="machine-learning.html#simple-regression"><i class="fa fa-check"></i><b>21.2</b> Simple Regression</a></li>
</ul></li>
<li class="chapter" data-level="22" data-path="cookies.html"><a href="cookies.html"><i class="fa fa-check"></i><b>22</b> Cookies</a>
<ul>
<li class="chapter" data-level="22.1" data-path="cookies.html"><a href="cookies.html#discover-js-cookie"><i class="fa fa-check"></i><b>22.1</b> Discover js-cookie</a></li>
<li class="chapter" data-level="22.2" data-path="custom-outputs.html"><a href="custom-outputs.html#setup"><i class="fa fa-check"></i><b>22.2</b> Setup</a></li>
<li class="chapter" data-level="22.3" data-path="prerequisites.html"><a href="prerequisites.html#javascript"><i class="fa fa-check"></i><b>22.3</b> JavaScript</a></li>
<li class="chapter" data-level="22.4" data-path="prerequisites.html"><a href="prerequisites.html#r-code"><i class="fa fa-check"></i><b>22.4</b> R Code</a></li>
</ul></li>
<li class="part"><span><b>VI Closing Remarks</b></span></li>
<li class="chapter" data-level="23" data-path="conclusion.html"><a href="conclusion.html"><i class="fa fa-check"></i><b>23</b> Conclusion</a>
<ul>
<li class="chapter" data-level="23.1" data-path="advanced-topics.html"><a href="advanced-topics.html#performances"><i class="fa fa-check"></i><b>23.1</b> Performances</a></li>
<li class="chapter" data-level="23.2" data-path="conclusion.html"><a href="conclusion.html#road-ahead"><i class="fa fa-check"></i><b>23.2</b> Road ahead</a>
<ul>
<li class="chapter" data-level="23.2.1" data-path="conclusion.html"><a href="conclusion.html#computation"><i class="fa fa-check"></i><b>23.2.1</b> Computation</a></li>
<li class="chapter" data-level="23.2.2" data-path="introduction.html"><a href="introduction.html#shiny"><i class="fa fa-check"></i><b>23.2.2</b> Shiny</a></li>
<li class="chapter" data-level="23.2.3" data-path="introduction.html"><a href="introduction.html#htmlwidgets"><i class="fa fa-check"></i><b>23.2.3</b> htmlwidgets</a></li>
</ul></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="ploty" class="section level1" number="11">
<h1><span class="header-section-number">Chapter 11</span> Ploty</h1>
<p>In this chapter we build a widget for plotly.js, or a tiny part of it; itβll allow drawing a scatter and line plot and have some additional functionalities in shiny. The aim of this chapter is to demonstrate how easily one can transfer take aways from building the gio widget to build another.</p>
<div id="discover" class="section level2" number="11.1">
<h2><span class="header-section-number">11.1</span> Discover</h2>
<p>Below is a basic example from the <a href="https://plotly.com/javascript/line-and-scatter/">plotly website</a>. We will aim to reproduce it using htmlwidgets before improving upon it.</p>
<div class="sourceCode" id="cb201"><pre class="sourceCode html"><code class="sourceCode html"><span id="cb201-1"><a href="ploty.html#cb201-1"></a><span class="dt"><!DOCTYPE </span>html<span class="dt">></span></span>
<span id="cb201-2"><a href="ploty.html#cb201-2"></a><span class="kw"><html</span><span class="ot"> xmlns=</span><span class="st">"http://www.w3.org/1999/xhtml"</span><span class="ot"> lang=</span><span class="st">""</span><span class="ot"> xml:lang=</span><span class="st">""</span><span class="kw">></span></span>
<span id="cb201-3"><a href="ploty.html#cb201-3"></a></span>
<span id="cb201-4"><a href="ploty.html#cb201-4"></a><span class="kw"><head></span></span>
<span id="cb201-5"><a href="ploty.html#cb201-5"></a> <span class="co"><!-- Import library --></span></span>
<span id="cb201-6"><a href="ploty.html#cb201-6"></a> <span class="kw"><script</span><span class="ot"> src=</span><span class="st">"plotly-latest.min.js"</span><span class="kw">></script></span></span>
<span id="cb201-7"><a href="ploty.html#cb201-7"></a><span class="kw"></head></span></span>
<span id="cb201-8"><a href="ploty.html#cb201-8"></a></span>
<span id="cb201-9"><a href="ploty.html#cb201-9"></a><span class="kw"><body></span></span>
<span id="cb201-10"><a href="ploty.html#cb201-10"></a> <span class="co"><!-- div to hold visualisation --></span></span>
<span id="cb201-11"><a href="ploty.html#cb201-11"></a> <span class="kw"><div</span><span class="ot"> id=</span><span class="st">"chart"</span><span class="ot"> style=</span><span class="st">"width:600px;height:250px;"</span><span class="kw">></div></span></span>
<span id="cb201-12"><a href="ploty.html#cb201-12"></a></span>
<span id="cb201-13"><a href="ploty.html#cb201-13"></a> <span class="co"><!-- Script to create visualisation --></span></span>
<span id="cb201-14"><a href="ploty.html#cb201-14"></a> <span class="kw"><script></span></span>
<span id="cb201-15"><a href="ploty.html#cb201-15"></a> <span class="kw">var</span> trace1 <span class="op">=</span> <span class="op">{</span></span>
<span id="cb201-16"><a href="ploty.html#cb201-16"></a> <span class="dt">x</span><span class="op">:</span> [<span class="dv">1</span><span class="op">,</span> <span class="dv">2</span><span class="op">,</span> <span class="dv">3</span><span class="op">,</span> <span class="dv">4</span>]<span class="op">,</span></span>
<span id="cb201-17"><a href="ploty.html#cb201-17"></a> <span class="dt">y</span><span class="op">:</span> [<span class="dv">10</span><span class="op">,</span> <span class="dv">15</span><span class="op">,</span> <span class="dv">13</span><span class="op">,</span> <span class="dv">17</span>]<span class="op">,</span></span>
<span id="cb201-18"><a href="ploty.html#cb201-18"></a> <span class="dt">mode</span><span class="op">:</span> <span class="st">'markers'</span><span class="op">,</span></span>
<span id="cb201-19"><a href="ploty.html#cb201-19"></a> <span class="dt">type</span><span class="op">:</span> <span class="st">'scatter'</span></span>
<span id="cb201-20"><a href="ploty.html#cb201-20"></a> <span class="op">};</span></span>
<span id="cb201-21"><a href="ploty.html#cb201-21"></a></span>
<span id="cb201-22"><a href="ploty.html#cb201-22"></a> <span class="kw">var</span> trace2 <span class="op">=</span> <span class="op">{</span></span>
<span id="cb201-23"><a href="ploty.html#cb201-23"></a> <span class="dt">x</span><span class="op">:</span> [<span class="dv">2</span><span class="op">,</span> <span class="dv">3</span><span class="op">,</span> <span class="dv">4</span><span class="op">,</span> <span class="dv">5</span>]<span class="op">,</span></span>
<span id="cb201-24"><a href="ploty.html#cb201-24"></a> <span class="dt">y</span><span class="op">:</span> [<span class="dv">16</span><span class="op">,</span> <span class="dv">5</span><span class="op">,</span> <span class="dv">11</span><span class="op">,</span> <span class="dv">9</span>]<span class="op">,</span></span>
<span id="cb201-25"><a href="ploty.html#cb201-25"></a> <span class="dt">mode</span><span class="op">:</span> <span class="st">'lines'</span><span class="op">,</span></span>
<span id="cb201-26"><a href="ploty.html#cb201-26"></a> <span class="dt">type</span><span class="op">:</span> <span class="st">'scatter'</span></span>
<span id="cb201-27"><a href="ploty.html#cb201-27"></a> <span class="op">};</span></span>
<span id="cb201-28"><a href="ploty.html#cb201-28"></a></span>
<span id="cb201-29"><a href="ploty.html#cb201-29"></a> <span class="kw">var</span> data <span class="op">=</span> [trace1<span class="op">,</span> trace2<span class="op">,</span> trace3]<span class="op">;</span></span>
<span id="cb201-30"><a href="ploty.html#cb201-30"></a></span>
<span id="cb201-31"><a href="ploty.html#cb201-31"></a> <span class="va">Plotly</span>.<span class="at">newPlot</span>(<span class="st">'chart'</span><span class="op">,</span> data)<span class="op">;</span></span>
<span id="cb201-32"><a href="ploty.html#cb201-32"></a> )<span class="op">;</span></span>
<span id="cb201-33"><a href="ploty.html#cb201-33"></a> <span class="kw"></script></span></span>
<span id="cb201-34"><a href="ploty.html#cb201-34"></a><span class="kw"></body></span></span>
<span id="cb201-35"><a href="ploty.html#cb201-35"></a></span>
<span id="cb201-36"><a href="ploty.html#cb201-36"></a><span class="kw"></html></span></span></code></pre></div>
<p>The aim is to obtain an R interface resembling the already existing R package. Itβll allow initialising the visualisation with the <code>plotly</code> function and add a line or scatter plot with <code>plot_line</code> and <code>plot_marker</code>.</p>
<div class="sourceCode" id="cb202"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb202-1"><a href="ploty.html#cb202-1"></a><span class="kw">plotly</span>(<span class="dt">data =</span> cars) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb202-2"><a href="ploty.html#cb202-2"></a><span class="st"> </span><span class="kw">plot_line</span>(<span class="dt">x =</span> dist, <span class="dt">y =</span> speed)</span></code></pre></div>
</div>
<div id="basics" class="section level2" number="11.2">
<h2><span class="header-section-number">11.2</span> Basics</h2>
<p>The story starts like all other widgets: create a package and scaffold the widget. We name the package plotlier so as to not override the local installation of the actual plotly package. We also add the magrittr pipe (<code>%>%</code>) while weβre at it.</p>
<div class="sourceCode" id="cb203"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb203-1"><a href="ploty.html#cb203-1"></a>usethis<span class="op">::</span><span class="kw">create_package</span>(<span class="st">"plotlier"</span>) <span class="co"># create package</span></span>
<span id="cb203-2"><a href="ploty.html#cb203-2"></a>usethis<span class="op">::</span><span class="kw">scaffoldWidget</span>(<span class="st">"plotly"</span>) <span class="co"># scaffold plotly</span></span>
<span id="cb203-3"><a href="ploty.html#cb203-3"></a>usethis<span class="op">::</span><span class="kw">use_pipe</span>() <span class="co"># export pipe</span></span></code></pre></div>
<p>Then we download the dependency from the CDN, the latest version at the time of writing this is <code>1.54.2</code>, and edit the <code>plotly.yml</code> file to point to the downloaded file.</p>
<div class="sourceCode" id="cb204"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb204-1"><a href="ploty.html#cb204-1"></a><span class="kw">dir.create</span>(<span class="st">"inst/htmlwidgets/plotly"</span>, <span class="dt">recursive =</span> <span class="ot">TRUE</span>)</span>
<span id="cb204-2"><a href="ploty.html#cb204-2"></a>url <-<span class="st"> "https://cdn.plot.ly/plotly-1.54.2.min.js"</span></span>
<span id="cb204-3"><a href="ploty.html#cb204-3"></a><span class="kw">download.file</span>(url, <span class="st">"inst/htmlwidgets/plotly/plotly.min.js"</span>)</span></code></pre></div>
<div class="sourceCode" id="cb205"><pre class="sourceCode yaml"><code class="sourceCode yaml"><span id="cb205-1"><a href="ploty.html#cb205-1"></a><span class="fu">dependencies</span><span class="kw">:</span></span>
<span id="cb205-2"><a href="ploty.html#cb205-2"></a><span class="at"> </span><span class="kw">-</span><span class="at"> </span><span class="fu">name</span><span class="kw">:</span><span class="at"> plotly</span></span>
<span id="cb205-3"><a href="ploty.html#cb205-3"></a><span class="at"> </span><span class="fu">version</span><span class="kw">:</span><span class="at"> </span><span class="fl">1.54.2</span></span>
<span id="cb205-4"><a href="ploty.html#cb205-4"></a><span class="at"> </span><span class="fu">src</span><span class="kw">:</span><span class="at"> htmlwidgets/plotly</span></span>
<span id="cb205-5"><a href="ploty.html#cb205-5"></a><span class="at"> </span><span class="fu">script</span><span class="kw">:</span><span class="at"> plotly.min.js</span></span></code></pre></div>
<p>We start by editing the <code>plotly</code> function to accept a data frame which will be stored in the <code>x</code> object. The data will not be used in this very function but will be required when adding a line or marker plot.</p>
<div class="sourceCode" id="cb206"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb206-1"><a href="ploty.html#cb206-1"></a><span class="co">#' @export</span></span>
<span id="cb206-2"><a href="ploty.html#cb206-2"></a>plotly <-<span class="st"> </span><span class="cf">function</span>(data, <span class="dt">width =</span> <span class="ot">NULL</span>, <span class="dt">height =</span> <span class="ot">NULL</span>, <span class="dt">elementId =</span> <span class="ot">NULL</span>) {</span>
<span id="cb206-3"><a href="ploty.html#cb206-3"></a></span>
<span id="cb206-4"><a href="ploty.html#cb206-4"></a> <span class="co"># forward options using x</span></span>
<span id="cb206-5"><a href="ploty.html#cb206-5"></a> x =<span class="st"> </span><span class="kw">list</span>(</span>
<span id="cb206-6"><a href="ploty.html#cb206-6"></a> <span class="dt">data =</span> data,</span>
<span id="cb206-7"><a href="ploty.html#cb206-7"></a> <span class="dt">options =</span> <span class="kw">list</span>()</span>
<span id="cb206-8"><a href="ploty.html#cb206-8"></a> )</span>
<span id="cb206-9"><a href="ploty.html#cb206-9"></a></span>
<span id="cb206-10"><a href="ploty.html#cb206-10"></a> <span class="co"># create widget</span></span>
<span id="cb206-11"><a href="ploty.html#cb206-11"></a> htmlwidgets<span class="op">::</span><span class="kw">createWidget</span>(</span>
<span id="cb206-12"><a href="ploty.html#cb206-12"></a> <span class="dt">name =</span> <span class="st">'plotly'</span>,</span>
<span id="cb206-13"><a href="ploty.html#cb206-13"></a> x,</span>
<span id="cb206-14"><a href="ploty.html#cb206-14"></a> <span class="dt">width =</span> width,</span>
<span id="cb206-15"><a href="ploty.html#cb206-15"></a> <span class="dt">height =</span> height,</span>
<span id="cb206-16"><a href="ploty.html#cb206-16"></a> <span class="dt">package =</span> <span class="st">'plotlier'</span>,</span>
<span id="cb206-17"><a href="ploty.html#cb206-17"></a> <span class="dt">elementId =</span> elementId</span>
<span id="cb206-18"><a href="ploty.html#cb206-18"></a> )</span>
<span id="cb206-19"><a href="ploty.html#cb206-19"></a>}</span></code></pre></div>
<p>The JavaScript would look like the code below. Itβs actually much easier than what had to be written for gio.js as the function to generate a new plot accepts 1) the id of the <code><div></code> where the plot should be created and 2) a JSON object of options (containing the data) which describes the plot.</p>
<div class="sourceCode" id="cb207"><pre class="sourceCode js"><code class="sourceCode javascript"><span id="cb207-1"><a href="ploty.html#cb207-1"></a><span class="va">HTMLWidgets</span>.<span class="at">widget</span>(<span class="op">{</span></span>
<span id="cb207-2"><a href="ploty.html#cb207-2"></a></span>
<span id="cb207-3"><a href="ploty.html#cb207-3"></a> <span class="dt">name</span><span class="op">:</span> <span class="st">'plotly'</span><span class="op">,</span></span>
<span id="cb207-4"><a href="ploty.html#cb207-4"></a></span>
<span id="cb207-5"><a href="ploty.html#cb207-5"></a> <span class="dt">type</span><span class="op">:</span> <span class="st">'output'</span><span class="op">,</span></span>
<span id="cb207-6"><a href="ploty.html#cb207-6"></a></span>
<span id="cb207-7"><a href="ploty.html#cb207-7"></a> <span class="dt">factory</span><span class="op">:</span> <span class="kw">function</span>(el<span class="op">,</span> width<span class="op">,</span> height) <span class="op">{</span></span>
<span id="cb207-8"><a href="ploty.html#cb207-8"></a></span>
<span id="cb207-9"><a href="ploty.html#cb207-9"></a> <span class="cf">return</span> <span class="op">{</span></span>
<span id="cb207-10"><a href="ploty.html#cb207-10"></a></span>
<span id="cb207-11"><a href="ploty.html#cb207-11"></a> <span class="dt">renderValue</span><span class="op">:</span> <span class="kw">function</span>(x) <span class="op">{</span></span>
<span id="cb207-12"><a href="ploty.html#cb207-12"></a></span>
<span id="cb207-13"><a href="ploty.html#cb207-13"></a> <span class="co">// create plot</span></span>
<span id="cb207-14"><a href="ploty.html#cb207-14"></a> <span class="va">Plotly</span>.<span class="at">newPlot</span>(<span class="va">el</span>.<span class="at">id</span><span class="op">,</span> <span class="va">x</span>.<span class="at">options</span>)<span class="op">;</span></span>
<span id="cb207-15"><a href="ploty.html#cb207-15"></a></span>
<span id="cb207-16"><a href="ploty.html#cb207-16"></a> <span class="op">},</span></span>
<span id="cb207-17"><a href="ploty.html#cb207-17"></a></span>
<span id="cb207-18"><a href="ploty.html#cb207-18"></a> <span class="dt">resize</span><span class="op">:</span> <span class="kw">function</span>(width<span class="op">,</span> height) <span class="op">{</span></span>
<span id="cb207-19"><a href="ploty.html#cb207-19"></a></span>
<span id="cb207-20"><a href="ploty.html#cb207-20"></a> <span class="co">// </span><span class="al">TODO</span><span class="co">: code to re-render the widget with a new size</span></span>
<span id="cb207-21"><a href="ploty.html#cb207-21"></a></span>
<span id="cb207-22"><a href="ploty.html#cb207-22"></a> <span class="op">}</span></span>
<span id="cb207-23"><a href="ploty.html#cb207-23"></a></span>
<span id="cb207-24"><a href="ploty.html#cb207-24"></a> <span class="op">};</span></span>
<span id="cb207-25"><a href="ploty.html#cb207-25"></a> <span class="op">}</span></span>
<span id="cb207-26"><a href="ploty.html#cb207-26"></a><span class="op">}</span>)<span class="op">;</span></span></code></pre></div>
<p>Then itβs a matter of reproducing the JSON that defines what plotly defines as a βtraceβ and what more or less corresponds to a βgeomβ in ggplot2. A trace for a scatter plot is defined with the JSON below, which we can read in R to understand the shape of the list to reproduce.</p>
<div class="sourceCode" id="cb208"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb208-1"><a href="ploty.html#cb208-1"></a>jsonlite<span class="op">::</span><span class="kw">fromJSON</span>(<span class="st">'{</span></span>
<span id="cb208-2"><a href="ploty.html#cb208-2"></a><span class="st"> "x": [1, 2, 3, 4],</span></span>
<span id="cb208-3"><a href="ploty.html#cb208-3"></a><span class="st"> "y": [10, 15, 13, 17],</span></span>
<span id="cb208-4"><a href="ploty.html#cb208-4"></a><span class="st"> "mode": "markers",</span></span>
<span id="cb208-5"><a href="ploty.html#cb208-5"></a><span class="st"> "type": "scatter"</span></span>
<span id="cb208-6"><a href="ploty.html#cb208-6"></a><span class="st">}'</span>)</span></code></pre></div>
<pre><code>## $x
## [1] 1 2 3 4
##
## $y
## [1] 10 15 13 17
##
## $mode
## [1] "markers"
##
## $type
## [1] "scatter"</code></pre>
<p>We can then create a function that takes the name of the x and y columns to fetch from the data which is accessible from the plotly object with <code>p$x$data</code></p>
<div class="sourceCode" id="cb210"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb210-1"><a href="ploty.html#cb210-1"></a><span class="co">#' @export</span></span>
<span id="cb210-2"><a href="ploty.html#cb210-2"></a>plot_marker <-<span class="st"> </span><span class="cf">function</span>(p, x, y){</span>
<span id="cb210-3"><a href="ploty.html#cb210-3"></a> layer <-<span class="st"> </span><span class="kw">list</span>(</span>
<span id="cb210-4"><a href="ploty.html#cb210-4"></a> <span class="dt">x =</span> dplyr<span class="op">::</span><span class="kw">pull</span>(p<span class="op">$</span>x<span class="op">$</span>data, {{ x }}),</span>
<span id="cb210-5"><a href="ploty.html#cb210-5"></a> <span class="dt">y =</span> dplyr<span class="op">::</span><span class="kw">pull</span>(p<span class="op">$</span>x<span class="op">$</span>data, {{ y }}),</span>
<span id="cb210-6"><a href="ploty.html#cb210-6"></a> <span class="dt">type =</span> <span class="st">"scatter"</span>,</span>
<span id="cb210-7"><a href="ploty.html#cb210-7"></a> <span class="dt">mode =</span> <span class="st">"markers"</span></span>
<span id="cb210-8"><a href="ploty.html#cb210-8"></a> )</span>
<span id="cb210-9"><a href="ploty.html#cb210-9"></a></span>
<span id="cb210-10"><a href="ploty.html#cb210-10"></a> p<span class="op">$</span>x<span class="op">$</span>options <-<span class="st"> </span><span class="kw">append</span>(p<span class="op">$</span>x<span class="op">$</span>options, <span class="kw">list</span>(layer))</span>
<span id="cb210-11"><a href="ploty.html#cb210-11"></a> <span class="kw">return</span>(p)</span>
<span id="cb210-12"><a href="ploty.html#cb210-12"></a>}</span></code></pre></div>
<p>At this stage the functions <code>devtools::document</code> and <code>devtools::load_all</code> can be run so the package can be tested.</p>
<div class="sourceCode" id="cb211"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb211-1"><a href="ploty.html#cb211-1"></a><span class="kw">library</span>(plotlier)</span>
<span id="cb211-2"><a href="ploty.html#cb211-2"></a></span>
<span id="cb211-3"><a href="ploty.html#cb211-3"></a><span class="kw">plotly</span>(cars) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb211-4"><a href="ploty.html#cb211-4"></a><span class="st"> </span><span class="kw">plot_marker</span>(dist, speed)</span></code></pre></div>
<div class="figure">
<img src="images/plotlier-scatter.png" alt="" />
<p class="caption">Plotlier Scatter plot</p>
</div>
</div>
<div id="javascript-to-r" class="section level2" number="11.3">
<h2><span class="header-section-number">11.3</span> JavaScript to R</h2>
<p>We can now look up plotlyβs documentation on <a href="https://plotly.com/javascript/plotlyjs-events/">callback</a> to send data to the R server when a point is clicked.</p>
<div class="sourceCode" id="cb212"><pre class="sourceCode js"><code class="sourceCode javascript"><span id="cb212-1"><a href="ploty.html#cb212-1"></a>...</span>
<span id="cb212-2"><a href="ploty.html#cb212-2"></a>renderValue<span class="op">:</span> <span class="kw">function</span>(x) <span class="op">{</span></span>
<span id="cb212-3"><a href="ploty.html#cb212-3"></a></span>
<span id="cb212-4"><a href="ploty.html#cb212-4"></a> plot <span class="op">=</span> <span class="va">document</span>.<span class="at">getElementById</span>(<span class="va">el</span>.<span class="at">id</span>)<span class="op">;</span></span>
<span id="cb212-5"><a href="ploty.html#cb212-5"></a> <span class="va">Plotly</span>.<span class="at">newPlot</span>(<span class="va">el</span>.<span class="at">id</span><span class="op">,</span> <span class="va">x</span>.<span class="at">options</span>)<span class="op">;</span></span>
<span id="cb212-6"><a href="ploty.html#cb212-6"></a></span>
<span id="cb212-7"><a href="ploty.html#cb212-7"></a> <span class="va">plot</span>.<span class="at">on</span>(<span class="st">'plotly_click'</span><span class="op">,</span> <span class="kw">function</span>(data)<span class="op">{</span></span>
<span id="cb212-8"><a href="ploty.html#cb212-8"></a> <span class="kw">var</span> coords <span class="op">=</span> [<span class="va">data</span>.<span class="at">points</span>[<span class="dv">0</span>].<span class="at">x</span><span class="op">,</span> <span class="va">data</span>.<span class="at">points</span>[<span class="dv">0</span>].<span class="at">y</span>]<span class="op">;</span></span>
<span id="cb212-9"><a href="ploty.html#cb212-9"></a> <span class="va">Shiny</span>.<span class="at">setInputValue</span>(<span class="va">el</span>.<span class="at">id</span> <span class="op">+</span> <span class="st">'_clicked'</span><span class="op">,</span> coords)<span class="op">;</span></span>
<span id="cb212-10"><a href="ploty.html#cb212-10"></a> <span class="op">}</span>)<span class="op">;</span></span>
<span id="cb212-11"><a href="ploty.html#cb212-11"></a></span>
<span id="cb212-12"><a href="ploty.html#cb212-12"></a><span class="op">}</span></span>
<span id="cb212-13"><a href="ploty.html#cb212-13"></a>...</span></code></pre></div>
<p>Finally, we load the packageβs functions using devtools to test the input.</p>
<div class="sourceCode" id="cb213"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb213-1"><a href="ploty.html#cb213-1"></a><span class="kw">library</span>(shiny)</span>
<span id="cb213-2"><a href="ploty.html#cb213-2"></a><span class="kw">library</span>(plotlier)</span>
<span id="cb213-3"><a href="ploty.html#cb213-3"></a></span>
<span id="cb213-4"><a href="ploty.html#cb213-4"></a>ui <-<span class="st"> </span><span class="kw">fluidPage</span>(</span>
<span id="cb213-5"><a href="ploty.html#cb213-5"></a> <span class="kw">plotlyOutput</span>(<span class="st">"plot"</span>),</span>
<span id="cb213-6"><a href="ploty.html#cb213-6"></a> <span class="kw">verbatimTextOutput</span>(<span class="st">"clicked"</span>)</span>
<span id="cb213-7"><a href="ploty.html#cb213-7"></a>)</span>
<span id="cb213-8"><a href="ploty.html#cb213-8"></a></span>
<span id="cb213-9"><a href="ploty.html#cb213-9"></a>server <-<span class="st"> </span><span class="cf">function</span>(input, output){</span>
<span id="cb213-10"><a href="ploty.html#cb213-10"></a></span>
<span id="cb213-11"><a href="ploty.html#cb213-11"></a> output<span class="op">$</span>plot <-<span class="st"> </span><span class="kw">renderPlotly</span>({</span>
<span id="cb213-12"><a href="ploty.html#cb213-12"></a> <span class="kw">plotly</span>(cars) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb213-13"><a href="ploty.html#cb213-13"></a><span class="st"> </span><span class="kw">plot_marker</span>(dist, speed)</span>
<span id="cb213-14"><a href="ploty.html#cb213-14"></a> })</span>
<span id="cb213-15"><a href="ploty.html#cb213-15"></a></span>
<span id="cb213-16"><a href="ploty.html#cb213-16"></a> output<span class="op">$</span>clicked <-<span class="st"> </span><span class="kw">renderPrint</span>({</span>
<span id="cb213-17"><a href="ploty.html#cb213-17"></a> <span class="kw">print</span>(input<span class="op">$</span>plot_clicked)</span>
<span id="cb213-18"><a href="ploty.html#cb213-18"></a> })</span>
<span id="cb213-19"><a href="ploty.html#cb213-19"></a></span>
<span id="cb213-20"><a href="ploty.html#cb213-20"></a>}</span>
<span id="cb213-21"><a href="ploty.html#cb213-21"></a></span>
<span id="cb213-22"><a href="ploty.html#cb213-22"></a><span class="kw">shinyApp</span>(ui, server)</span></code></pre></div>
</div>
<div id="r-to-javascript" class="section level2" number="11.4">
<h2><span class="header-section-number">11.4</span> R to JavaScript</h2>
<p>Here we implement an example of passing data from R to JavaScript, name the JavaScript <code>addTraces</code> function which allows dynamically adding traces to the chart without redrawing it in its entirety.</p>
<p>The JavaScript <code>addTraces</code> function takes the id of the chart to add the trace to and a JSON object that defines the trace.</p>
<div class="sourceCode" id="cb214"><pre class="sourceCode js"><code class="sourceCode javascript"><span id="cb214-1"><a href="ploty.html#cb214-1"></a><span class="co">// addTraces example</span></span>
<span id="cb214-2"><a href="ploty.html#cb214-2"></a><span class="va">Plotly</span>.<span class="at">addTraces</span>(<span class="st">'plot'</span><span class="op">,</span> <span class="op">{</span><span class="dt">y</span><span class="op">:</span> [<span class="dv">2</span><span class="op">,</span><span class="dv">1</span><span class="op">,</span><span class="dv">2</span>]<span class="op">}</span>)<span class="op">;</span></span></code></pre></div>
<p>We start, as done previously, by defining a βproxy,β a function that returns an object containing information necessary to add traces to the chart, namely the <code>id</code> of the chart to apply to and the <code>session</code> object which will be used to send the data.</p>
<div class="sourceCode" id="cb215"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb215-1"><a href="ploty.html#cb215-1"></a><span class="co">#' @export</span></span>
<span id="cb215-2"><a href="ploty.html#cb215-2"></a>plotlyProxy <-<span class="st"> </span><span class="cf">function</span>(id, <span class="dt">session =</span> shiny<span class="op">::</span><span class="kw">getDefaultReactiveDomain</span>()){</span>
<span id="cb215-3"><a href="ploty.html#cb215-3"></a> <span class="kw">list</span>(</span>
<span id="cb215-4"><a href="ploty.html#cb215-4"></a> <span class="dt">id =</span> id,</span>
<span id="cb215-5"><a href="ploty.html#cb215-5"></a> <span class="dt">session =</span> session</span>
<span id="cb215-6"><a href="ploty.html#cb215-6"></a> )</span>
<span id="cb215-7"><a href="ploty.html#cb215-7"></a>}</span></code></pre></div>
<p>We then create a function that creates the trace and sends that data, together with the plot <code>id</code>, to JavaScript as a message of type βadd-traces.β</p>
<div class="sourceCode" id="cb216"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb216-1"><a href="ploty.html#cb216-1"></a><span class="co">#' @export</span></span>
<span id="cb216-2"><a href="ploty.html#cb216-2"></a>plot_add_marker <-<span class="st"> </span><span class="cf">function</span>(proxy, data, x, y){</span>
<span id="cb216-3"><a href="ploty.html#cb216-3"></a> data <-<span class="st"> </span><span class="kw">list</span>(</span>
<span id="cb216-4"><a href="ploty.html#cb216-4"></a> <span class="dt">x =</span> dplyr<span class="op">::</span><span class="kw">pull</span>(data, {{ x }}),</span>
<span id="cb216-5"><a href="ploty.html#cb216-5"></a> <span class="dt">y =</span> dplyr<span class="op">::</span><span class="kw">pull</span>(data, {{ y }}),</span>
<span id="cb216-6"><a href="ploty.html#cb216-6"></a> <span class="dt">type =</span> <span class="st">"scatter"</span>,</span>
<span id="cb216-7"><a href="ploty.html#cb216-7"></a> <span class="dt">mode =</span> <span class="st">"markers"</span></span>
<span id="cb216-8"><a href="ploty.html#cb216-8"></a> )</span>
<span id="cb216-9"><a href="ploty.html#cb216-9"></a></span>
<span id="cb216-10"><a href="ploty.html#cb216-10"></a> msg <-<span class="st"> </span><span class="kw">list</span>(<span class="dt">id =</span> proxy<span class="op">$</span>id, <span class="dt">data =</span> data)</span>
<span id="cb216-11"><a href="ploty.html#cb216-11"></a> proxy<span class="op">$</span>session<span class="op">$</span><span class="kw">sendCustomMessage</span>(<span class="st">"add-traces"</span>, msg)</span>
<span id="cb216-12"><a href="ploty.html#cb216-12"></a> <span class="kw">return</span>(proxy)</span>
<span id="cb216-13"><a href="ploty.html#cb216-13"></a>}</span></code></pre></div>
<p>Finally, we add a message handler to handle the data sent to <code>add-traces</code> and use the JavaScript method <code>addTraces</code> to add a trace to the plot.</p>
<div class="sourceCode" id="cb217"><pre class="sourceCode js"><code class="sourceCode javascript"><span id="cb217-1"><a href="ploty.html#cb217-1"></a><span class="cf">if</span>(<span class="va">HTMLWidgets</span>.<span class="at">shinyMode</span>)<span class="op">{</span></span>
<span id="cb217-2"><a href="ploty.html#cb217-2"></a></span>
<span id="cb217-3"><a href="ploty.html#cb217-3"></a> <span class="va">Shiny</span>.<span class="at">addCustomMessageHandler</span>(type <span class="op">=</span> <span class="st">'add-traces'</span><span class="op">,</span> <span class="kw">function</span>(msg)<span class="op">{</span></span>
<span id="cb217-4"><a href="ploty.html#cb217-4"></a> <span class="va">Plotly</span>.<span class="at">addTraces</span>(<span class="va">msg</span>.<span class="at">id</span><span class="op">,</span> <span class="va">msg</span>.<span class="at">data</span>)<span class="op">;</span></span>
<span id="cb217-5"><a href="ploty.html#cb217-5"></a> <span class="op">}</span>)</span>
<span id="cb217-6"><a href="ploty.html#cb217-6"></a></span>
<span id="cb217-7"><a href="ploty.html#cb217-7"></a><span class="op">}</span></span></code></pre></div>
<p>After installing the package, one can use the new proxy function in a shiny application, below we provide a button that adds a trace of random data to the chart.</p>
<div class="sourceCode" id="cb218"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb218-1"><a href="ploty.html#cb218-1"></a><span class="kw">library</span>(shiny)</span>
<span id="cb218-2"><a href="ploty.html#cb218-2"></a><span class="kw">library</span>(plotlier)</span>
<span id="cb218-3"><a href="ploty.html#cb218-3"></a></span>
<span id="cb218-4"><a href="ploty.html#cb218-4"></a>df <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="dv">1</span><span class="op">:</span><span class="dv">10</span>, <span class="dt">y =</span> <span class="kw">runif</span>(<span class="dv">10</span>))</span>
<span id="cb218-5"><a href="ploty.html#cb218-5"></a></span>
<span id="cb218-6"><a href="ploty.html#cb218-6"></a>ui <-<span class="st"> </span><span class="kw">fluidPage</span>(</span>
<span id="cb218-7"><a href="ploty.html#cb218-7"></a> <span class="kw">actionButton</span>(<span class="st">"add"</span>, <span class="st">"Add random trace"</span>),</span>
<span id="cb218-8"><a href="ploty.html#cb218-8"></a> <span class="kw">plotlyOutput</span>(<span class="st">"plot"</span>)</span>
<span id="cb218-9"><a href="ploty.html#cb218-9"></a>)</span>
<span id="cb218-10"><a href="ploty.html#cb218-10"></a></span>
<span id="cb218-11"><a href="ploty.html#cb218-11"></a>server <-<span class="st"> </span><span class="cf">function</span>(input, output){</span>
<span id="cb218-12"><a href="ploty.html#cb218-12"></a></span>
<span id="cb218-13"><a href="ploty.html#cb218-13"></a> output<span class="op">$</span>plot <-<span class="st"> </span><span class="kw">renderPlotly</span>({</span>
<span id="cb218-14"><a href="ploty.html#cb218-14"></a> df <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb218-15"><a href="ploty.html#cb218-15"></a><span class="st"> </span><span class="kw">plotly</span>() <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb218-16"><a href="ploty.html#cb218-16"></a><span class="st"> </span><span class="kw">plot_line</span>(x, y)</span>
<span id="cb218-17"><a href="ploty.html#cb218-17"></a> })</span>
<span id="cb218-18"><a href="ploty.html#cb218-18"></a></span>
<span id="cb218-19"><a href="ploty.html#cb218-19"></a> proxy <-<span class="st"> </span><span class="kw">plotlyProxy</span>(<span class="st">"plot"</span>)</span>
<span id="cb218-20"><a href="ploty.html#cb218-20"></a></span>
<span id="cb218-21"><a href="ploty.html#cb218-21"></a> <span class="kw">observeEvent</span>(input<span class="op">$</span>add, {</span>
<span id="cb218-22"><a href="ploty.html#cb218-22"></a> random <-<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">x =</span> <span class="kw">runif</span>(<span class="dv">10</span>, <span class="dv">1</span>, <span class="dv">10</span>), <span class="dt">y =</span> <span class="kw">runif</span>(<span class="dv">10</span>))</span>
<span id="cb218-23"><a href="ploty.html#cb218-23"></a> </span>
<span id="cb218-24"><a href="ploty.html#cb218-24"></a> <span class="kw">plot_add_marker</span>(</span>
<span id="cb218-25"><a href="ploty.html#cb218-25"></a> proxy,</span>
<span id="cb218-26"><a href="ploty.html#cb218-26"></a> random,</span>
<span id="cb218-27"><a href="ploty.html#cb218-27"></a> x, y</span>
<span id="cb218-28"><a href="ploty.html#cb218-28"></a> )</span>
<span id="cb218-29"><a href="ploty.html#cb218-29"></a> })</span>
<span id="cb218-30"><a href="ploty.html#cb218-30"></a></span>
<span id="cb218-31"><a href="ploty.html#cb218-31"></a>}</span>
<span id="cb218-32"><a href="ploty.html#cb218-32"></a></span>
<span id="cb218-33"><a href="ploty.html#cb218-33"></a><span class="kw">shinyApp</span>(ui, server)</span></code></pre></div>
<div class="figure">
<img src="images/plotlier-add.png" alt="" />
<p class="caption">Plotly with dynamic traces</p>
</div>
</div>
<div id="size-resize" class="section level2" number="11.5">
<h2><span class="header-section-number">11.5</span> Size & Resize</h2>
<p>At this stage the plotlier package does yet provide responsive visualisation: resizing the screen does not resize the visualisation. In order to ensure that it is we have to define a default width that is not defined in pixel but percentage (such as 100%) and implement the resize method.</p>
<p>As mentioned in a previous chapter, the default size can be changed with the <code>sizingPolicy</code> function, below we set it to <code>100%</code>.</p>
<div class="sourceCode" id="cb219"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb219-1"><a href="ploty.html#cb219-1"></a><span class="co"># create widget</span></span>
<span id="cb219-2"><a href="ploty.html#cb219-2"></a>htmlwidgets<span class="op">::</span><span class="kw">createWidget</span>(</span>
<span id="cb219-3"><a href="ploty.html#cb219-3"></a> <span class="dt">name =</span> <span class="st">'plotly'</span>,</span>
<span id="cb219-4"><a href="ploty.html#cb219-4"></a> x,</span>
<span id="cb219-5"><a href="ploty.html#cb219-5"></a> <span class="dt">width =</span> width,</span>
<span id="cb219-6"><a href="ploty.html#cb219-6"></a> <span class="dt">height =</span> height,</span>
<span id="cb219-7"><a href="ploty.html#cb219-7"></a> <span class="dt">package =</span> <span class="st">'plotlier'</span>,</span>
<span id="cb219-8"><a href="ploty.html#cb219-8"></a> <span class="dt">elementId =</span> elementId,</span>
<span id="cb219-9"><a href="ploty.html#cb219-9"></a> <span class="dt">preRenderHook =</span> render_plotlier,</span>
<span id="cb219-10"><a href="ploty.html#cb219-10"></a> <span class="dt">sizingPolicy =</span> <span class="kw">sizingPolicy</span>(</span>
<span id="cb219-11"><a href="ploty.html#cb219-11"></a> <span class="dt">defaultWidth =</span> <span class="st">"100%"</span></span>
<span id="cb219-12"><a href="ploty.html#cb219-12"></a> )</span>
<span id="cb219-13"><a href="ploty.html#cb219-13"></a>)</span></code></pre></div>
<p>Next we can use the plotly function <code>relayout</code> to resize the visualisation when the size of its container changes.</p>
<div class="sourceCode" id="cb220"><pre class="sourceCode js"><code class="sourceCode javascript"><span id="cb220-1"><a href="ploty.html#cb220-1"></a>resize<span class="op">:</span> <span class="kw">function</span>(width<span class="op">,</span> height) <span class="op">{</span></span>
<span id="cb220-2"><a href="ploty.html#cb220-2"></a> <span class="va">Plotly</span>.<span class="at">relayout</span>(<span class="va">el</span>.<span class="at">id</span><span class="op">,</span> <span class="op">{</span><span class="dt">width</span><span class="op">:</span> width<span class="op">,</span> <span class="dt">height</span><span class="op">:</span> height<span class="op">}</span>)<span class="op">;</span></span>
<span id="cb220-3"><a href="ploty.html#cb220-3"></a><span class="op">}</span></span></code></pre></div>
<p>One can test this by resizing the browser or RStudio viewer window.</p>
<div class="sourceCode" id="cb221"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb221-1"><a href="ploty.html#cb221-1"></a><span class="kw">plotly</span>(cars) <span class="op">%>%</span><span class="st"> </span></span>
<span id="cb221-2"><a href="ploty.html#cb221-2"></a><span class="st"> </span><span class="kw">plot_marker</span>(<span class="st">"dist"</span>, <span class="st">"speed"</span>)</span></code></pre></div>
<div class="figure">
<img src="images/plotlier-responsive.png" alt="" />
<p class="caption">Responsive plotly output</p>
</div>
</div>
<div id="performance-security" class="section level2" number="11.6">
<h2><span class="header-section-number">11.6</span> Performance & Security</h2>
<p>The package stores the data passed to the <code>plotly</code> function in the widget object, however, as explained in a previous chapter this is insecure and a drain on performances. First, this data serialised to JSON but not used in JavaScript and thus leads to unnecessary computations. Second, this data is visible on the resulting visualisation which may cause security concerns given the user may not deliberately use all the data in the visualisation. This can be remedied to by use the <code>preRenderHook</code>.</p>
<div class="sourceCode" id="cb222"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb222-1"><a href="ploty.html#cb222-1"></a>render_plotlier <-<span class="st"> </span><span class="cf">function</span>(p){</span>
<span id="cb222-2"><a href="ploty.html#cb222-2"></a> p<span class="op">$</span>x<span class="op">$</span>data <-<span class="st"> </span><span class="ot">NULL</span></span>
<span id="cb222-3"><a href="ploty.html#cb222-3"></a> <span class="kw">return</span>(p)</span>
<span id="cb222-4"><a href="ploty.html#cb222-4"></a>}</span></code></pre></div>
<p>The function define above can then be used in the <code>htmlwidgets::createWidget</code> function so it is run just before the visualisation renders.</p>
<div class="sourceCode" id="cb223"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb223-1"><a href="ploty.html#cb223-1"></a>htmlwidgets<span class="op">::</span><span class="kw">createWidget</span>(</span>
<span id="cb223-2"><a href="ploty.html#cb223-2"></a> <span class="dt">name =</span> <span class="st">'plotly'</span>,</span>
<span id="cb223-3"><a href="ploty.html#cb223-3"></a> x,</span>
<span id="cb223-4"><a href="ploty.html#cb223-4"></a> <span class="dt">width =</span> width,</span>
<span id="cb223-5"><a href="ploty.html#cb223-5"></a> <span class="dt">height =</span> height,</span>
<span id="cb223-6"><a href="ploty.html#cb223-6"></a> <span class="dt">package =</span> <span class="st">'plotlier'</span>,</span>
<span id="cb223-7"><a href="ploty.html#cb223-7"></a> <span class="dt">elementId =</span> elementId,</span>
<span id="cb223-8"><a href="ploty.html#cb223-8"></a> <span class="dt">preRenderHook =</span> render_plotlier</span>
<span id="cb223-9"><a href="ploty.html#cb223-9"></a>)</span></code></pre></div>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="wrap-up.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="discover.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/3-28-htmlwidgets-plotly.Rmd",
"text": "Edit"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": {},
"toc": {
"collapse": "section"
}
});
});
</script>
</body>
</html>