From f94645ce8eed9df3a705ba10eaaf241f42ccd102 Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Fri, 17 Jun 2016 12:02:05 -0500 Subject: [PATCH] trace type inference --- R/plotly_build.R | 9 ++++----- R/utils.R | 40 ++++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 11 deletions(-) diff --git a/R/plotly_build.R b/R/plotly_build.R index cb75c1ac35..4f9d389954 100644 --- a/R/plotly_build.R +++ b/R/plotly_build.R @@ -54,10 +54,6 @@ plotly_build.plotly <- function(p) { dats <- Map(function(x, y) { - x$type <- verify_type(x$type) - - d <- plotly_data(p, y) - # add sensible axis names to layout for (i in c("x", "y", "z")) { nm <- paste0(i, "axis") @@ -73,12 +69,15 @@ plotly_build.plotly <- function(p) { } # perform the evaluation + d <- plotly_data(p, y) x <- rapply(x, eval_attr, data = d, how = "list") + x <- verify_type(x) + attrLengths <- lengths(x) # if appropriate, set the mode now since we need to reference it later - if (grepl("scatter", x$type)) { + if (grepl("scatter", x$type) && is.null(x$mode)) { x$mode <- if (any(attrLengths > 20)) "lines" else "markers+lines" } diff --git a/R/utils.R b/R/utils.R index c39bbfad09..bcbccaac29 100644 --- a/R/utils.R +++ b/R/utils.R @@ -118,22 +118,50 @@ verify_box <- function(proposed, schema) { proposed } + # make sure trace type is valid # TODO: add an argument to verify trace properties are valid (https://github.com/ropensci/plotly/issues/540) -verify_type <- function(type = NULL) { - if (is.null(type)) { - message("No trace type specified. Using the 'scatter' default.") - type <- "scatter" +verify_type <- function(trace) { + if (is.null(trace$type)) { + attrs <- names(trace) + attrLengths <- lengths(trace) + if (all(c("x", "y", "z") %in% attrs)) { + trace$type <- if (all(c("i", "j", "k") %in% attrs)) relay_type("mesh3d") else relay_type("scatter3d") + } else if (all(c("x", "y") %in% attrs)) { + if (is.numeric(trace$x) && is.numeric(trace$y)) { + trace$type <- if (any(attrLengths) > 15000) relay_type("scattergl") else relay_type("scatter") + } else if (is.numeric(trace$x)) { + trace$type <- relay_type("bar") + trace$orientation <- "h" + } else if (is.numeric(trace$y)) { + trace$type <- relay_type("bar") + } else { + trace$type <- relay_type("histogram2d") + } + } else if ("y" %in% attrs || "x" %in% attrs) { + trace$type <- relay_type("histogram") + } else { + warning("No trace type specified and no positional attributes specified", call. = FALSE) + trace$type <- relay_type("scatter") + } } - if (!is.character(type) || length(type) != 1) { + if (!is.character(trace$type) || length(trace$type) != 1) { stop("The trace type must be a character vector of length 1.\n", call. = FALSE) } - if (!type %in% names(Schema$traces)) { + if (!trace$type %in% names(Schema$traces)) { stop("Trace type must be one of the following: \n", "'", paste(names(Schema$traces), collapse = "', '"), "'", call. = FALSE) } + trace +} + +relay_type <- function(type) { + message( + "No trace type specified. Inferring a type of '", type, "'.\n", + "Read more about this trace type here -> https://plot.ly/r/reference/#", type + ) type }