Skip to content

Commit

Permalink
trace type inference
Browse files Browse the repository at this point in the history
  • Loading branch information
cpsievert committed Jun 17, 2016
1 parent 5e3f011 commit f94645c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
9 changes: 4 additions & 5 deletions R/plotly_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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"
}

Expand Down
40 changes: 34 additions & 6 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")

This comment has been minimized.

Copy link
@timelyportfolio

timelyportfolio Jul 12, 2016

Collaborator

add_scatter no longer a function

} 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")

This comment has been minimized.

Copy link
@timelyportfolio

timelyportfolio Jul 12, 2016

Collaborator

add_scatter no longer a function

}
}
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
}

Expand Down

0 comments on commit f94645c

Please sign in to comment.