Skip to content

Commit f94645c

Browse files
committed
trace type inference
1 parent 5e3f011 commit f94645c

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

R/plotly_build.R

+4-5
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,6 @@ plotly_build.plotly <- function(p) {
5454

5555
dats <- Map(function(x, y) {
5656

57-
x$type <- verify_type(x$type)
58-
59-
d <- plotly_data(p, y)
60-
6157
# add sensible axis names to layout
6258
for (i in c("x", "y", "z")) {
6359
nm <- paste0(i, "axis")
@@ -73,12 +69,15 @@ plotly_build.plotly <- function(p) {
7369
}
7470

7571
# perform the evaluation
72+
d <- plotly_data(p, y)
7673
x <- rapply(x, eval_attr, data = d, how = "list")
7774

75+
x <- verify_type(x)
76+
7877
attrLengths <- lengths(x)
7978

8079
# if appropriate, set the mode now since we need to reference it later
81-
if (grepl("scatter", x$type)) {
80+
if (grepl("scatter", x$type) && is.null(x$mode)) {
8281
x$mode <- if (any(attrLengths > 20)) "lines" else "markers+lines"
8382
}
8483

R/utils.R

+34-6
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,50 @@ verify_box <- function(proposed, schema) {
118118
proposed
119119
}
120120

121+
121122
# make sure trace type is valid
122123
# TODO: add an argument to verify trace properties are valid (https://github.com/ropensci/plotly/issues/540)
123-
verify_type <- function(type = NULL) {
124-
if (is.null(type)) {
125-
message("No trace type specified. Using the 'scatter' default.")
126-
type <- "scatter"
124+
verify_type <- function(trace) {
125+
if (is.null(trace$type)) {
126+
attrs <- names(trace)
127+
attrLengths <- lengths(trace)
128+
if (all(c("x", "y", "z") %in% attrs)) {
129+
trace$type <- if (all(c("i", "j", "k") %in% attrs)) relay_type("mesh3d") else relay_type("scatter3d")
130+
} else if (all(c("x", "y") %in% attrs)) {
131+
if (is.numeric(trace$x) && is.numeric(trace$y)) {
132+
trace$type <- if (any(attrLengths) > 15000) relay_type("scattergl") else relay_type("scatter")
133+
} else if (is.numeric(trace$x)) {
134+
trace$type <- relay_type("bar")
135+
trace$orientation <- "h"
136+
} else if (is.numeric(trace$y)) {
137+
trace$type <- relay_type("bar")
138+
} else {
139+
trace$type <- relay_type("histogram2d")
140+
}
141+
} else if ("y" %in% attrs || "x" %in% attrs) {
142+
trace$type <- relay_type("histogram")
143+
} else {
144+
warning("No trace type specified and no positional attributes specified", call. = FALSE)
145+
trace$type <- relay_type("scatter")
146+
}
127147
}
128-
if (!is.character(type) || length(type) != 1) {
148+
if (!is.character(trace$type) || length(trace$type) != 1) {
129149
stop("The trace type must be a character vector of length 1.\n",
130150
call. = FALSE)
131151
}
132-
if (!type %in% names(Schema$traces)) {
152+
if (!trace$type %in% names(Schema$traces)) {
133153
stop("Trace type must be one of the following: \n",
134154
"'", paste(names(Schema$traces), collapse = "', '"), "'",
135155
call. = FALSE)
136156
}
157+
trace
158+
}
159+
160+
relay_type <- function(type) {
161+
message(
162+
"No trace type specified. Inferring a type of '", type, "'.\n",
163+
"Read more about this trace type here -> https://plot.ly/r/reference/#", type
164+
)
137165
type
138166
}
139167

0 commit comments

Comments
 (0)