-
Notifications
You must be signed in to change notification settings - Fork 631
/
Copy pathplotly_build.R
1022 lines (898 loc) · 38.9 KB
/
plotly_build.R
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
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#' 'Build' (i.e., evaluate) a plotly object
#'
#' This generic function creates the list object sent to plotly.js
#' for rendering. Using this function can be useful for overriding defaults
#' provided by `ggplotly`/`plot_ly` or for debugging rendering
#' errors.
#'
#' @param p a ggplot object, or a plotly object, or a list.
#' @param registerFrames should a frame trace attribute be interpreted as frames in an animation?
#' @export
#' @examples
#'
#' p <- plot_ly(economics, x = ~date, y = ~pce)
#' # the unevaluated plotly object
#' str(p)
#' # the evaluated data
#' str(plotly_build(p)$x$data)
#'
plotly_build <- function(p, registerFrames = TRUE) {
UseMethod("plotly_build")
}
#' @export
plotly_build.NULL <- function(...) {
htmltools::browsable(htmltools::div(...))
}
#' @export
plotly_build.list <- function(p, registerFrames = TRUE) {
plotly_build(as_widget(p))
}
#' @export
plotly_build.gg <- function(p, registerFrames = TRUE) {
# note: since preRenderHook = plotly_build in as_widget(),
# plotly_build.plotly() will be called on gg objects as well
plotly_build(ggplotly(p))
}
#' @export
plotly_build.plotly <- function(p, registerFrames = TRUE) {
# make this plot retrievable
set_last_plot(p)
layouts <- Map(function(x, y) {
d <- plotly_data(p, y)
x <- rapply(x, eval_attr, data = d, how = "list")
# if an annotation attribute is an array, expand into multiple annotations
nAnnotations <- max(lengths(x$annotations) %||% 0)
if (!is.null(names(x$annotations))) {
# font is the only list object, so store it, and attach after transposing
font <- x$annotations[["font"]]
x$annotations <- purrr::transpose(lapply(x$annotations, function(x) {
as.list(rep(x, length.out = nAnnotations))
}))
for (i in seq_len(nAnnotations)) {
x$annotations[[i]][["font"]] <- font
}
}
x[lengths(x) > 0]
}, p$x$layoutAttrs, names2(p$x$layoutAttrs))
# get rid of the data -> layout mapping
p$x$layoutAttrs <- NULL
# accumulate, rather than override, annotations.
annotations <- Reduce(c, c(
list(p$x$layout$annotations),
setNames(compact(lapply(layouts, "[[", "annotations")), NULL)
))
# merge layouts into a single layout (more recent layouts will override older ones)
p$x$layout <- modify_list(p$x$layout, Reduce(modify_list, layouts))
p$x$layout$annotations <- annotations
# keep frame mapping for populating layout.slider.currentvalue in animations
frameMapping <- unique(unlist(
lapply(p$x$attrs, function(x) deparse2(x[["frame"]])),
use.names = FALSE
))
if (length(frameMapping) > 1) {
warning("Only one `frame` variable is allowed", call. = FALSE)
}
# Attributes should be NULL if none exist (rather than an empty list)
if (length(p$x$attrs) == 0) p$x$attrs <- NULL
# If there is just one (unevaluated) trace, and the data is sf, add an sf layer
if (length(p$x$attrs) == 1 && !inherits(p$x$attrs[[1]], "plotly_eval") && is_sf(plotly_data(p))) {
p <- add_sf(p)
}
# If type was not specified in plot_ly(), it doesn't create a trace unless
# there are no other traces
if (is.null(p$x$attrs[[1]][["type"]]) && length(p$x$attrs) > 1) {
p$x$attrs[[1]] <- NULL
}
# have the attributes already been evaluated?
is.evaled <- function(x) inherits(x, "plotly_eval")
attrsToEval <- p$x$attrs[!vapply(p$x$attrs, is.evaled, logical(1))]
# trace type checking and renaming for plot objects
if (is_mapbox(p) || is_geo(p)) {
p <- geo2cartesian(p)
attrsToEval <- lapply(attrsToEval, function(tr) {
if (!grepl("scatter|choropleth", tr[["type"]] %||% "scatter")) {
stop("Cant add a '", tr[["type"]], "' trace to a map object", call. = FALSE)
}
if (is_mapbox(p)) tr[["type"]] <- "scattermapbox"
if (is_geo(p)) {
tr[["type"]] <- if (!is.null(tr[["z"]])) "choropleth" else "scattergeo"
}
tr
})
}
dats <- Map(function(x, y) {
# grab the data for this trace
dat <- plotly_data(p, y)
# formula/symbol/attribute evaluation
trace <- structure(
rapply(x, eval_attr, data = dat, how = "list"),
class = oldClass(x)
)
# attach crosstalk info, if necessary
if (crosstalk_key() %in% names(dat) && isTRUE(trace[["inherit"]] %||% TRUE)) {
trace[["key"]] <- trace[["key"]] %||% dat[[crosstalk_key()]]
trace[["set"]] <- trace[["set"]] %||% attr(dat, "set")
}
# if appropriate, tack on a group index
grps <- tryCatch(
as.character(dplyr::groups(dat)),
error = function(e) character(0)
)
if (length(grps) && any(lengths(trace) == NROW(dat))) {
trace[[".plotlyGroupIndex"]] <- interaction(dat[, grps, drop = F])
}
# determine trace type (if not specified, can depend on the # of data points)
# note that this should also determine a sensible mode, if appropriate
trace <- verify_type(trace)
# verify orientation of boxes/bars
trace <- verify_orientation(trace)
# add sensible axis names to layout
for (i in c("x", "y", "z")) {
nm <- paste0(i, "axis")
idx <- which(names(trace) %in% i)
if (length(idx) == 1) {
title <- default(deparse2(x[[idx]]))
if (is3d(trace$type) || i == "z") {
p$x$layout$scene[[nm]]$title <<- p$x$layout$scene[[nm]]$title %||% title
} else {
p$x$layout[[nm]]$title <<- p$x$layout[[nm]]$title %||% title
}
}
}
if (inherits(trace, c("plotly_surface", "plotly_contour"))) {
# TODO: generate matrix for users?
# (1) if z is vector, and x/y are null throw error
# (2) if x/y/z are vectors and length(x) * length(y) == length(z), convert z to matrix
if (!is.matrix(trace[["z"]]) || !is.numeric(trace[["z"]])) {
stop("`z` must be a numeric matrix", call. = FALSE)
}
}
# collect non-positional scales, plotly.js data_arrays, and "special"
# array attributes for "data training"
Attrs <- Schema$traces[[trace[["type"]]]]$attributes
isArray <- vapply(Attrs, function(x) {
tryFALSE(identical(x[["valType"]], "data_array"))
}, logical(1))
arrayOk <- vapply(Attrs, function(x) tryNULL(x[["arrayOk"]]) %||% FALSE, logical(1))
# "non-tidy" traces allow x/y of different lengths, so ignore those
dataArrayAttrs <- if (is_tidy(trace)) names(Attrs)[isArray | arrayOk]
allAttrs <- c(
dataArrayAttrs, special_attrs(trace), npscales(), "frame",
# for some reason, text isn't listed as a data array in some traces
# I'm looking at you scattergeo...
".plotlyGroupIndex", "text", "key", "fillcolor", "name", "legendgroup"
)
tr <- trace[names(trace) %in% allAttrs]
# TODO: does it make sense to "train" matrices/2D-tables (e.g. z)?
tr <- tr[vapply(tr, function(x) is.null(dim(x)) && is.atomic(x), logical(1))]
builtData <- tibble::as_tibble(tr)
# avoid clobbering I() (i.e., variables that shouldn't be scaled)
for (i in seq_along(tr)) {
if (inherits(tr[[i]], "AsIs")) builtData[[i]] <- I(builtData[[i]])
}
if (NROW(builtData) > 0) {
# Build the index used to split one "trace" into multiple traces
isAsIs <- vapply(builtData, function(x) inherits(x, "AsIs"), logical(1))
isDiscrete <- vapply(builtData, is.discrete, logical(1))
# note: can only have one linetype per trace
isSplit <- names(builtData) %in% c("split", "linetype", "frame", "fillcolor", "name") |
!isAsIs & isDiscrete & names(builtData) %in% c("symbol", "color")
if (any(isSplit)) {
paste2 <- function(x, y) if (identical(x, y)) x else paste(x, y, sep = br())
splitVars <- builtData[isSplit]
builtData[[".plotlyTraceIndex"]] <- Reduce(paste2, splitVars)
# in registerFrames() we need to strip the frame from .plotlyTraceIndex
# so keep track of which variable it is...
trace$frameOrder <- which(names(splitVars) %in% "frame")
}
# Build the index used to determine grouping (later on, NAs are inserted
# via group2NA() to create the groups). This is done in 3 parts:
# 1. Sort data by the trace index since groups are nested within traces.
# 2. Translate missing values on positional scales to a grouping variable.
# If grouping isn't relevant for this trace, a warning is thrown since
# NAs are removed.
# 3. The grouping from (2) and any groups detected via dplyr::groups()
# are combined into a single grouping variable, .plotlyGroupIndex
builtData <- arrange_safe(builtData, ".plotlyTraceIndex")
isComplete <- complete.cases(builtData[names(builtData) %in% c("x", "y", "z")])
# warn about missing values if groups aren't relevant for this trace type
if (any(!isComplete) && !has_group(trace)) {
warning("Ignoring ", sum(!isComplete), " observations", call. = FALSE)
}
builtData[[".plotlyMissingIndex"]] <- cumsum(!isComplete)
builtData <- builtData[isComplete, ]
if (length(grps) && has_group(trace) && isTRUE(trace[["connectgaps"]])) {
stop(
"Can't use connectgaps=TRUE when data has group(s).", call. = FALSE
)
}
builtData[[".plotlyGroupIndex"]] <- interaction(
builtData[[".plotlyGroupIndex"]] %||% "",
builtData[[".plotlyMissingIndex"]]
)
builtData <- arrange_safe(builtData,
c(".plotlyTraceIndex", ".plotlyGroupIndex",
if (inherits(trace, "plotly_line")) "x")
)
builtData <- train_data(builtData, trace)
trace[[".plotlyVariableMapping"]] <- names(builtData)
# copy over to the trace data
for (i in names(builtData)) {
trace[[i]] <- builtData[[i]]
}
}
# TODO: provide a better way to clean up "high-level" attrs
trace[c("ymin", "ymax", "yend", "xend")] <- NULL
trace[lengths(trace) > 0]
}, attrsToEval, names2(attrsToEval))
p$x$attrs <- lapply(p$x$attrs, function(x) structure(x, class = "plotly_eval"))
# traceify by the interaction of discrete variables
traces <- list()
for (i in seq_along(dats)) {
d <- dats[[i]]
scaleAttrs <- names(d) %in% paste0(npscales(), "s")
traces <- c(traces, traceify(d[!scaleAttrs], d$.plotlyTraceIndex))
if (i == 1) traces[[1]] <- c(traces[[1]], d[scaleAttrs])
}
# insert NAs to differentiate groups
traces <- lapply(traces, function(x) {
d <- data.frame(x[names(x) %in% x$.plotlyVariableMapping], stringsAsFactors = FALSE)
d <- group2NA(
d, if (has_group(x)) ".plotlyGroupIndex",
ordered = if (inherits(x, "plotly_line")) "x",
retrace.first = inherits(x, "plotly_polygon")
)
for (i in x$.plotlyVariableMapping) {
# try to reduce the amount of data we have to send for non-positional scales
x[[i]] <- structure(
if (i %in% npscales()) uniq(d[[i]]) else d[[i]],
class = oldClass(x[[i]])
)
}
x
})
# Map special plot_ly() arguments to plotly.js trace attributes.
# Note that symbol/linetype can modify the mode, so those are applied first
# TODO: use 'legends 2.0' to create legends for these discrete mappings
# https://github.com/plotly/plotly.js/issues/1668
if (length(traces)) {
traces <- map_symbol(traces)
traces <- map_linetype(traces)
traces <- map_size(traces)
traces <- map_size(traces, stroke = TRUE) #i.e., span
colorTitle <- unlist(lapply(p$x$attrs, function(x) { deparse2(x[["color"]] %||% x[["z"]]) }))
strokeTitle <- unlist(lapply(p$x$attrs, function(x) deparse2(x[["stroke"]])))
traces <- map_color(traces, title = paste(colorTitle, collapse = br()), colorway = colorway(p))
traces <- map_color(traces, stroke = TRUE, title = paste(strokeTitle, collapse = br()), colorway = colorway(p))
}
for (i in seq_along(traces)) {
# remove special mapping attributes
mappingAttrs <- c(
"alpha", "alpha_stroke", npscales(), paste0(npscales(), "s"),
".plotlyGroupIndex", ".plotlyMissingIndex",
".plotlyTraceIndex", ".plotlyVariableMapping", "inherit"
)
for (j in mappingAttrs) {
traces[[i]][[j]] <- NULL
}
}
# .crossTalkKey -> key
traces <- lapply(traces, function(x) {
setNames(x, sub(crosstalk_key(), "key", names(x), fixed = TRUE))
})
# it's possible that the plot object already has some traces
# (like figures pulled from a plotly server)
p$x$data <- setNames(c(p$x$data, traces), NULL)
# supply linked highlighting options/features
p <- supply_highlight_attrs(p)
# supply trace anchor and domain information
p <- supply_defaults(p)
# attribute naming corrections for "geo-like" traces
p <- cartesian2geo(p)
# Compute sensible bounding boxes for each mapbox/geo subplot
p <- fit_bounds(p)
# polar charts don't like null width/height keys
if (is.null(p$x$layout[["height"]])) p$x$layout[["height"]] <- NULL
if (is.null(p$x$layout[["width"]])) p$x$layout[["width"]] <- NULL
# ensure we get the order of categories correct
# (plotly.js uses the order in which categories appear by default)
p <- populate_categorical_axes(p)
# translate '\n' to '<br />' in text strings
p <- translate_linebreaks(p)
# if it makes sense, add markers/lines/text to mode
p <- verify_mode(p)
# annotations & shapes must be an array of objects
# TODO: should we add anything else to this?
p <- verify_arrays(p)
# set a sensible hovermode if it hasn't been specified already
p <- verify_hovermode(p)
# try to convert to webgl if toWebGl was used
p <- verify_webgl(p)
# crosstalk dynamically adds traces, meaning that a legend could be dynamically
# added, which is confusing. So here we populate a sensible default.
p <- verify_showlegend(p)
# NOTE: this needs to occur *before* registering frames so simple/nested key
# flags get passed onto frame data.
p <- verify_key_type(p)
if (registerFrames) {
p <- registerFrames(p, frameMapping = frameMapping)
}
p <- verify_guides(p)
# verify plot attributes are legal according to the plotly.js spec
p <- verify_attr_names(p)
# box up 'data_array' attributes where appropriate
p <- verify_attr_spec(p)
verify_mathjax <- function(p) {
hasMathjax <- "mathjax" %in% sapply(p$dependencies, "[[", "name")
if (hasMathjax) return(p)
hasTeX <- any(rapply(p$x, is.TeX))
if (!hasTeX) return(p)
# TODO: it would be much better to add the dependency here, but
# htmlwidgets doesn't currently support adding dependencies at print-time!
warning(
"Detected the use of `TeX()`, but mathjax has not been specified. ",
"Try running `config(.Last.value, mathjax = 'cdn')`",
call. = FALSE
)
p
}
# make sure we're including mathjax (if TeX() is used)
p <- verify_mathjax(p)
# if a partial bundle was specified, make sure it supports the visualization
p <- verify_partial_bundle(p)
# make sure plots don't get sent out of the network (for enterprise)
p$x$base_url <- get_domain()
p
}
# ----------------------------------------------------------------
# Functions used solely within plotly_build
# ----------------------------------------------------------------
registerFrames <- function(p, frameMapping = NULL) {
# ensure one frame value per trace, and if its missing, insert NA
p$x$data <- lapply(p$x$data, function(tr) {
tr[["frame"]] <- tr[["frame"]][[1]] %||% NA
tr
})
# the ordering of this object determines the ordering of the frames
frameAttrs <- unlist(lapply(p$x$data, "[[", "frame"))
# NOTE: getLevels() should drop NAs
frameNames <- getLevels(frameAttrs)
p$x$data <- lapply(p$x$data, function(tr) { tr$frame <- as.character(tr$frame); tr })
# remove frames from the trace names
for (i in seq_along(p$x$data)) {
tr <- p$x$data[[i]]
if (length(tr[["name"]]) != 1) next
nms <- strsplit(as.character(tr[["name"]]), br())[[1]]
idx <- setdiff(seq_along(nms), tr$frameOrder %||% 0)
p$x$data[[i]]$name <- if (length(idx)) paste(nms[idx], collapse = br()) else NULL
p$x$data[[i]]$frameOrder <- NULL
}
# exit in trivial cases
nFrames <- length(frameNames)
if (nFrames < 2) return(p)
# --------------------------------------------------------------------------
# set a "global" range of x/y (TODO: handle multiple axes?)
# --------------------------------------------------------------------------
x <- unlist(lapply(p$x$data, function(x) x[["x"]]))
if (is.numeric(x)) {
rng <- range(x, na.rm = TRUE)
if (identical(p$x$layout$xaxis$type, "log")) {
rng <- log10(rng)
rng[is.nan(rng)] <- 0
}
p$x$layout$xaxis$range <- p$x$layout$xaxis$range %||% extendrange(rng)
}
y <- unlist(lapply(p$x$data, function(x) x[["y"]]))
if (is.numeric(y)) {
rng <- range(y, na.rm = TRUE)
if (identical(p$x$layout$yaxis$type, "log")) {
rng <- log10(rng)
rng[is.nan(rng)] <- 0
}
p$x$layout$yaxis$range <- p$x$layout$yaxis$range %||% extendrange(rng)
}
# --------------------------------------------------------------------------
# Similar to setting a global x/y range, we need a "global trace range"
#
# implementation details via @rreusser: frames specify *state changes*,
# so if frame 1 has 3 traces, and frame 2 has 2 traces,
# we need to explicity supply 3 traces
# in both frames, but make 1 invisible in frame 2. For example,
# http://codepen.io/cpsievert/pen/gmXVWe
# For that reason, every frame (including the "initial" frame) has the
# max # of traces and "missing traces" are not visible (i.e., `visible=false`)
# --------------------------------------------------------------------------
# remember, at this point, frame has been removed from the trace name
frameTraceNames <- unique(unlist(lapply(p$x$data[!is.na(frameAttrs)], "[[", "name")))
for (i in seq_along(frameNames)) {
nm <- frameNames[[i]]
d <- p$x$data[sapply(p$x$data, "[[", "frame") %in% nm]
# ensure, the frames API knows what is visible/invisible
d <- lapply(d, function(tr) { tr$visible <- tr$visible %||% TRUE; tr })
# if this frame is missing a trace name, supply an invisible one
traceNamesMissing <- setdiff(frameTraceNames, sapply(d, "[[", "name"))
for (j in traceNamesMissing) {
idx <- vapply(p$x$data, function(tr) isTRUE(tr[["name"]] == j), logical(1))
idx <- which(idx)[[1]]
invisible <- modify_list(p$x$data[[idx]], list(visible = FALSE))
d <- c(d, list(invisible))
}
p$x$frames[[i]] <- list(
name = as.character(format(nm)),
data = lapply(d, function(tr) {
spec <- Schema$traces[[tr$type %||% "scatter"]]$attributes
verify_attr(tr, spec)
})
)
}
# ensure the plot knows about the "global trace range"
firstFrame <- vapply(p$x$data, function(tr) isTRUE(tr[["frame"]] %in% frameNames[[1]]), logical(1))
p$x$data[firstFrame] <- p$x$frames[[1]]$data
# remove frame traces
idx <- vapply(p$x$data, function(tr) isTRUE(tr[["frame"]] %in% frameNames[-1]), logical(1))
p$x$data[idx] <- NULL
# this works since we now have a global trace range
p$x$frames <- lapply(p$x$frames, function(f) {
f$traces <- i(which(!is.na(sapply(p$x$data, "[[", "frame"))) - 1)
f
})
# retrain color defaults
p$x$data <- colorway_retrain(p$x$data, colorway(p))
p$x$frames <- lapply(p$x$frames, function(f) {
f$data <- colorway_retrain(f$data, colorway(p)[f$traces + 1])
f
})
# populate layout.sliders.currentvalue with a sensible default
defaultvalue <- if (length(frameMapping) == 1) {
list(
prefix = paste0(frameMapping, ": "),
xanchor = 'right',
font = list(
size = 16,
color = toRGB("gray80")
)
)
} else NULL
# supply animation option defaults (a la, highlight_defaults())
p$animation <- p$animation %||% animation_opts_defaults()
# if all the frame trace data are scatter traces, set a default of redraw=F
types <- unique(unlist(lapply(p$x$frames, function(f) {
vapply(f$data, function(tr) tr$type %||% "scatter", character(1))
})))
if (identical(types, "scatter") && is.default(p$animation$frame$redraw)) {
p$animation$frame$redraw <- default(FALSE)
}
# _always_ display an animation button and slider by default
animation_button_supply(
animation_slider_supply(p, currentvalue = defaultvalue)
)
}
train_data <- function(data, trace) {
if (inherits(trace, "plotly_ribbon")) {
data <- ribbon_dat(data)
}
if (inherits(trace, "plotly_segment")) {
# TODO: this could be faster, more efficient
data$.plotlyGroupIndex <- seq_len(NROW(data))
idx <- rep(seq_len(NROW(data)), each = 2)
dat <- as.data.frame(data[!grepl("^xend$|^yend", names(data))])
dat <- dat[idx, ]
idx2 <- seq.int(2, NROW(dat), by = 2)
dat[idx2, "x"] <- data[["xend"]]
dat[idx2, "y"] <- data[["yend"]]
data <- dplyr::group_by_(dat, ".plotlyGroupIndex", add = TRUE)
}
# TODO: a lot more geoms!!!
data
}
map_size <- function(traces, stroke = FALSE) {
sizeList <- lapply(traces, "[[", if (stroke) "span" else "size")
nSizes <- lengths(sizeList)
# if no "top-level" size is present, return traces untouched
if (all(nSizes == 0)) return(traces)
allSize <- unlist(compact(sizeList))
if (!is.null(allSize) && is.discrete(allSize)) {
stop("`size`/`width` values must be numeric .", call. = FALSE)
}
sizeRange <- range(allSize, na.rm = TRUE)
mapSize <- switch(
if (stroke) "span" else "size",
span = function(trace, sizes) {
type <- trace$type %||% "scatter"
size_ <- uniq(sizes)
isSingular <- length(size_) == 1
attrs <- Schema$traces[[type]]$attributes
# `span` controls marker.line.width
if (has_attr(type, "marker")) {
s <- if (isSingular) size_ else if (array_ok(attrs$marker$line$width)) sizes
trace$marker$line <- modify_list(list(width = default(s)), trace$marker$line)
}
# `span` controls error_[x/y].thickness
for (attr in c("error_y", "error_x")) {
if (!has_attr(type, attr)) next
s <- if (isSingular) size_ else if (array_ok(attrs[[attr]]$thickness)) sizes
trace[[attr]] <- modify_list(list(thickness = default(s)), trace[[attr]])
}
# When fill exists, `span` controls line.width
if (has_fill(trace) && has_attr(type, "line")) {
s <- if (isSingular) size_ else if (array_ok(attrs$line$width)) sizes else NA
if (is.na(s)) {
warning("`line.width` does not currently support multiple values.", call. = FALSE)
} else {
trace[["line"]] <- modify_list(list(width = default(s)), trace[["line"]])
}
}
trace
},
size = function(trace, sizes) {
type <- trace$type %||% "scatter"
size_ <- uniq(sizes)
isSingular <- length(size_) == 1
attrs <- Schema$traces[[type]]$attributes
# `size` controls marker.size (note 'bar' traces have marker but not marker.size)
# TODO: always ensure an array? https://github.com/ropensci/plotly/pull/1176
if (has_attr(type, "marker") && "size" %in% names(attrs$marker)) {
s <- if (isSingular) size_ else if (array_ok(attrs$marker$size)) sizes
trace$marker <- modify_list(list(size = default(s), sizemode = default("area")), trace$marker)
}
# `size` controls textfont.size
if (has_attr(type, "textfont")) {
s <- if (isSingular) size_ else if (array_ok(attrs$textfont$size)) sizes
trace$textfont <- modify_list(list(size = default(s)), trace$textfont)
}
# `size` controls error_[x/y].width
for (attr in c("error_y", "error_x")) {
if (!has_attr(type, attr)) next
s <- if (isSingular) size_ else if (array_ok(attrs[[attr]]$width)) sizes
trace[[attr]] <- modify_list(list(width = default(s)), trace[[attr]])
}
# if fill does not exist, `size` controls line.width
if (!has_fill(trace) && has_attr(type, "line")) {
s <- if (isSingular) size_ else if (array_ok(attrs$line$width)) sizes else NA
if (is.na(s)) {
warning("`line.width` does not currently support multiple values.", call. = FALSE)
} else {
trace[["line"]] <- modify_list(list(width = default(s)), trace[["line"]])
}
}
trace
}
)
for (i in which(nSizes > 0)) {
s <- sizeList[[i]]
isConstant <- inherits(s, "AsIs")
sizeI <- if (isConstant) {
structure(s, class = setdiff(class(s), "AsIs"))
} else {
to <- if (stroke) traces[[1]][["spans"]] else traces[[1]][["sizes"]]
scales::rescale(s, from = sizeRange, to = to)
}
traces[[i]] <- mapSize(traces[[i]], sizeI)
}
traces
}
# appends a new (empty) trace to generate (plot-wide) colorbar/colorscale
map_color <- function(traces, stroke = FALSE, title = "", colorway, na.color = "transparent") {
color <- if (stroke) {
lapply(traces, function(x) { x[["stroke"]] %||% x[["color"]] })
} else {
lapply(traces, function(x) { x[["color"]] %||% if (grepl("histogram2d", x[["type"]])) c(0, 1) else if (has_attr(x[["type"]], "colorscale")) x[["surfacecolor"]] %||% x[["z"]] })
}
alphas <- if (stroke) {
vapply(traces, function(x) x$alpha_stroke %||% 1, numeric(1))
} else {
vapply(traces, function(x) x[["alpha"]] %||% 1, numeric(1))
}
isConstant <- vapply(color, function(x) inherits(x, "AsIs") || is.null(x), logical(1))
isNumeric <- vapply(color, is.numeric, logical(1)) & !isConstant
isDiscrete <- vapply(color, is.discrete, logical(1)) & !isConstant
if (any(isNumeric & isDiscrete)) stop("Can't have both discrete and numeric color mappings", call. = FALSE)
uniqColor <- lapply(color, uniq)
isSingular <- lengths(uniqColor) == 1
# color/colorscale/colorbar attribute placement depends on trace type and marker mode
# TODO: remove these and make numeric colorscale mapping more like the rest
types <- vapply(traces, function(tr) tr$type %||% "scatter", character(1))
modes <- vapply(traces, function(tr) tr$mode %||% "lines", character(1))
hasLine <- has_line(types, modes)
hasLineColor <- has_color_array(types, "line")
hasText <- has_text(types, modes)
hasTextColor <- has_color_array(types, "text")
hasZ <- has_attr(types, "colorscale") & !stroke &
any(vapply(traces, function(tr) {
!is.null(tr[["z"]]) || grepl("histogram2d", tr[["type"]])
}, logical(1)))
# IDEA - attach color codes whether they make sense, unless there is a
# vector of color codes and the target is a constant
mapColor <- switch(
if (stroke) "stroke" else "fill",
stroke = function(trace, rgba, is_colorway = FALSE) {
type <- trace$type %||% "scatter"
rgba_ <- uniq(rgba)
isSingular <- length(rgba_) == 1
attrs <- Schema$traces[[type]]$attributes
default_ <- if (is_colorway) function(x) prefix_class(default(x), "colorway") else default
if (has_attr(type, "marker")) {
col <- if (isSingular) rgba_ else if (array_ok(attrs$marker$line$color)) rgba
trace$marker$line <- modify_list(list(color = default_(col)), trace$marker$line)
}
if (has_fill(trace)) {
col <- if (isSingular) rgba_ else if (array_ok(attrs$line$color)) rgba
if (is.null(col)) {
warning("`line.color` does not currently support multiple values.", call. = FALSE)
} else {
trace$line <- modify_list(list(color = default_(col)), trace$line)
}
}
trace
},
fill = function(trace, rgba, is_colorway = FALSE) {
type <- trace$type %||% "scatter"
rgba_ <- uniq(rgba)
isSingular <- length(rgba_) == 1
attrs <- Schema$traces[[type]]$attributes
default_ <- if (is_colorway) function(x) prefix_class(default(x), "colorway") else default
# `color` controls marker.color, textfont.color, error_[x/y].color
# TODO: any more attributes that make sense to include here?
for (attr in c("marker", "textfont", "error_y", "error_x")) {
if (!has_attr(type, attr)) next
if (is_colorway && "textfont" == attr) next
col <- if (isSingular) rgba_ else if (array_ok(attrs[[attr]]$color)) rgba else NA
if (is.na(col)) {
warning("`", attr, ".color` does not currently support multiple values.", call. = FALSE)
} else {
trace[[attr]] <- modify_list(list(color = default_(col)), trace[[attr]])
}
}
# If trace has fill, `color` controls fillcolor; otherwise line.color
if (has_fill(trace)) {
if (!isSingular) warning("Only one fillcolor per trace allowed", call. = FALSE)
# alpha defaults to 0.5 when applied to fillcolor
if (is.null(trace[["alpha"]])) rgba_ <- toRGB(rgba_, 0.5)
if (isSingular) trace <- modify_list(list(fillcolor = default_(rgba_)), trace)
} else if (has_attr(type, "line")) {
# if fill does not exist, 'color' controls line.color
col <- if (isSingular) rgba_ else if (array_ok(attrs$line$color)) rgba else NA
if (is.na(col)) {
warning("`line.color` does not currently support multiple values.", call. = FALSE)
} else {
trace[["line"]] <- modify_list(list(color = default_(col)), trace[["line"]])
}
}
trace
}
)
# i.e., interpret values as color codes
if (any(isConstant)) {
colorCodes <- Map(`%||%`, color, rep(colorway, length.out = length(traces)))
colorCodes <- Map(toRGB, colorCodes[isConstant], alphas[isConstant])
isColorway <- lengths(color[isConstant]) == 0
traces[isConstant] <- Map(mapColor, traces[isConstant], colorCodes, isColorway)
}
# since stroke inherits from color, it should inherit the palette too
palette <- if (stroke) traces[[1]][["strokes"]] %||% traces[[1]][["colors"]] else traces[[1]][["colors"]]
if (any(isDiscrete)) {
# unlist() does _not_ preserve order factors
isOrdered <- all(vapply(color[isDiscrete], is.ordered, logical(1)))
lvls <- getLevels(unlist(color[isDiscrete]))
N <- length(lvls)
pal <- palette %||% if (isOrdered) viridisLite::viridis(N) else RColorBrewer::brewer.pal(N, "Set2")
colScale <- scales::col_factor(pal, levels = names(pal) %||% lvls, na.color = na.color)
color_codes <- Map(function(x, y) toRGB(colScale(as.character(x)), y), color[isDiscrete], alphas[isDiscrete])
traces[isDiscrete] <- Map(mapColor, traces[isDiscrete], color_codes)
}
if (any(isNumeric)) {
pal <- palette %||% viridisLite::viridis(10)
# TODO: use ggstat::frange() when it's on CRAN?
allColor <- unlist(color[isNumeric])
rng <- range(allColor, na.rm = TRUE)
colScale <- scales::col_numeric(pal, rng, na.color = na.color)
# generate the colorscale to be shared across traces
vals <- if (diff(rng) > 0) {
as.numeric(stats::quantile(allColor, probs = seq(0, 1, length.out = 25), na.rm = TRUE))
} else {
c(0, 1)
}
colorScale <- matrix(
c(scales::rescale(vals), toRGB(colScale(vals), alphas[[1]])),
ncol = 2
)
colorObj <- list(
colorbar = lapply(list(title = as.character(title), ticklen = 2), default),
cmin = default(rng[1]),
cmax = default(rng[2]),
colorscale = default(colorScale),
showscale = default(FALSE)
)
for (i in which(isNumeric)) {
# when colorscale is being attached to `z`, we don't need color values in
# colorObj, so create colorbar trace now and exit early
if (hasZ[[i]]) {
colorObj[c("cmin", "cmax")] <- NULL
colorObj[["showscale"]] <- default(TRUE)
traces[[i]] <- modify_list(colorObj, traces[[i]])
traces[[i]]$colorscale <- as_df(traces[[i]]$colorscale)
# sigh, contour colorscale doesn't support alpha
if (grepl("contour", traces[[i]][["type"]])) {
traces[[i]]$colorscale[, 2] <- strip_alpha(traces[[i]]$colorscale[, 2])
}
traces[[i]] <- structure(traces[[i]], class = c("plotly_colorbar", "zcolor"))
next
}
# if trace is singular (i.e., one unique color in this trace), then there
# is no need for a colorscale, and both stroke/color have relevancy
if (isSingular[[i]]) {
col <- colScale(uniq(color[[i]]))
traces[[i]] <- mapColor(traces[[i]], toRGB(col, alphas[[i]]))
} else {
colorObj$color <- default(color[[i]])
if (stroke) {
traces[[i]]$marker$line <- modify_list(colorObj, traces[[i]]$marker$line)
} else {
traces[[i]]$marker <- modify_list(colorObj, traces[[i]]$marker)
}
if (hasLine[[i]]) {
if (hasLineColor[[i]]) {
traces[[i]]$line <- modify_list(colorObj, traces[[i]]$line)
} else {
warning("line.color doesn't (yet) support data arrays", call. = FALSE)
}
}
if (hasText[[i]]) {
if (hasTextColor[[i]]) {
traces[[i]]$textfont <- modify_list(colorObj, traces[[i]]$textfont)
} else {
warning("textfont.color doesn't (yet) support data arrays", call. = FALSE)
}
}
# TODO: how to make the summary stat (mean) customizable?
if (has_fill(traces[[i]])) {
warning("Only one fillcolor per trace allowed", call. = FALSE)
col <- toRGB(colScale(mean(colorObj$color, na.rm = TRUE)), alphas[[i]])
if (is.null(traces[[i]][["alpha"]])) col <- toRGB(col, 0.5)
traces[[i]] <- modify_list(list(fillcolor = col), traces[[i]])
}
# make sure the colorscale is going to convert to JSON nicely
traces[[i]]$marker$colorscale <- as_df(traces[[i]]$marker$colorscale)
}
}
# exit early if no additional colorbar trace is needed
if (any(hasZ)) return(traces)
if (stroke && sum(lengths(lapply(traces, "[[", "stroke"))) == 0) return(traces)
# add an "empty" trace with the colorbar
colorObj$color <- rng
colorObj$showscale <- default(TRUE)
colorBarTrace <- list(
x = range(unlist(lapply(traces, "[[", "x")), na.rm = TRUE),
y = range(unlist(lapply(traces, "[[", "y")), na.rm = TRUE),
type = if (any(types %in% glTypes())) "scattergl" else "scatter",
mode = "markers",
opacity = 0,
hoverinfo = "none",
showlegend = FALSE,
marker = colorObj
)
# 3D needs a z property
if ("scatter3d" %in% types) {
colorBarTrace$type <- "scatter3d"
colorBarTrace$z <- range(unlist(lapply(traces, "[[", "z")), na.rm = TRUE)
}
if (length(type <- intersect(c("scattergeo", "scattermapbox"), types))) {
colorBarTrace$type <- type
colorBarTrace$lat <- colorBarTrace$y
colorBarTrace$lon <- colorBarTrace$x
colorBarTrace[["x"]] <- NULL
colorBarTrace[["y"]] <- NULL
}
traces[[length(traces) + 1]] <- structure(colorBarTrace, class = "plotly_colorbar")
}
traces
}
map_symbol <- function(traces) {
symbolList <- lapply(traces, "[[", "symbol")
nSymbols <- lengths(symbolList)
# if no "top-level" symbol is present, return traces untouched
if (all(nSymbols == 0)) {
return(traces)
}
symbol <- unlist(compact(symbolList))
lvls <- getLevels(symbol)
# get a sensible default palette (also throws warnings)
pal <- setNames(scales::shape_pal()(length(lvls)), lvls)
pal <- supplyUserPalette(pal, traces[[1]][["symbols"]])
validSymbols <- as.character(Schema$traces$scatter$attributes$marker$symbol$values)
for (i in which(nSymbols > 0)) {
s <- symbolList[[i]]
symbols <- pch2symbol(if (inherits(s, "AsIs")) s else as.character(pal[as.character(s)]))
illegalSymbols <- setdiff(symbols, validSymbols)
if (length(illegalSymbols)) {
warning(
"The following are not valid symbol codes:\n'",
paste(illegalSymbols, collapse = "', '"), "'\n",
"Valid symbols include:\n'",
paste(validSymbols, collapse = "', '"), call. = FALSE
)
}
traces[[i]][["marker"]] <- modify_list(
list(symbol = default(symbols)), traces[[i]][["marker"]]
)
# ensure the mode is set so that the symbol is relevant
if (!grepl("markers", traces[[i]]$mode %||% "")) {
message("Adding markers to mode; otherwise symbol would have no effect.")
traces[[i]]$mode <- paste0(traces[[i]]$mode, "+markers")
}
}
traces
}
map_linetype <- function(traces) {
linetypeList <- lapply(traces, "[[", "linetype")
nLinetypes <- lengths(linetypeList)
# if no "top-level" linetype is present, return traces untouched
if (all(nLinetypes == 0)) return(traces)
linetype <- unlist(compact(linetypeList))
lvls <- getLevels(linetype)
# get a sensible default palette
pal <- setNames(scales::linetype_pal()(length(lvls)), lvls)
pal <- supplyUserPalette(pal, traces[[1]][["linetypes"]])
validLinetypes <- as.character(Schema$traces$scatter$attributes$line$dash$values)
if (length(pal) > length(validLinetypes)) {
warning("plotly.js only supports 6 different linetypes", call. = FALSE)
}
for (i in which(nLinetypes > 0)) {
l <- linetypeList[[i]]
dashes <- lty2dash(if (inherits(l, "AsIs")) l else as.character(pal[as.character(l)]))
illegalLinetypes <- setdiff(dashes, validLinetypes)
if (length(illegalLinetypes)) {
warning(
"The following are not valid linetype codes:\n'",
paste(illegalLinetypes, collapse = "', '"), "'\n",
"Valid linetypes include:\n'",
paste(validLinetypes, collapse = "', '"), "'", call. = FALSE
)
}
traces[[i]][["line"]] <- modify_list(
list(dash = default(dashes)), traces[[i]][["line"]]
)
# ensure the mode is set so that the linetype is relevant
if (!grepl("lines", traces[[i]]$mode %||% "")) {
message("Adding lines to mode; otherwise linetype would have no effect.")
traces[[i]][["mode"]] <- paste0(traces[[i]][["mode"]], "+lines")
}
}
traces
}
# break up a single trace into multiple traces according to values stored
# a particular key name
traceify <- function(dat, x = NULL) {
if (length(x) == 0) return(list(dat))
lvls <- if (is.factor(x)) levels(x) else unique(x)
lvls <- lvls[lvls %in% x]
# the order of lvls determines the order in which traces are drawn
# for ordered factors at least, it makes sense to draw the highest level first
# since that _should_ be the darkest color in a sequential pallette
if (is.ordered(x)) lvls <- rev(lvls)
n <- length(x)
# recursively search for a non-list of appropriate length (if it is, subset it)
recurse <- function(z, n, idx) {
if (is.list(z)) lapply(z, recurse, n, idx) else if (length(z) == n) z[idx] else z
}
new_dat <- list()
for (j in seq_along(lvls)) {
new_dat[[j]] <- lapply(dat, function(y) recurse(y, n, x %in% lvls[j]))
new_dat[[j]]$name <- new_dat[[j]]$name %||% lvls[j]