Skip to content

Commit dc4c6cd

Browse files
committed
store all ggplot2 layer info in attrs/visdat; only eval unevaluated attrs
1 parent b1589d1 commit dc4c6cd

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

R/ggplotly.R

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -757,23 +757,33 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
757757
# returns list element with their 'AsIs' class,
758758
# which conflicts with our JSON unboxing strategy.
759759
l <- rm_asis(l)
760-
l$cur_data <- new_id()
761-
# translate "plot-wide" aesthetic mappings to formulas so plotly_build()
762-
# understands them
763-
mappingFormulas <- if (originalData) {
764-
lapply(plot$mapping, lazyeval::f_new)
765-
} else {
766-
nms <- names(plot$mapping)
767-
setNames(lapply(nms, function(x) lazyeval::f_new(as.symbol(x))), nms)
768-
}
769-
dat <- if (originalData) plot$data else data[[layerData]]
770-
if (!is.null(mappingFormulas[["group"]])) {
771-
dat <- dplyr::group_by_(dat, mappingFormulas[["group"]])
772-
}
760+
761+
# start build a plotly object with meta information about the ggplot
762+
ids <- lapply(seq_along(data), function(x) new_id())
763+
l$cur_data <- ids[[layerData]]
764+
l$visdat <- if (originalData) lapply(layer_data, function(x) function(y) x) else lapply(data, function(x) function(y) x)
765+
l$visdat <- setNames(l$visdat, ids)
766+
767+
# translate layer mappings -> plotly attrs
768+
mappingFormulas <- lapply(layers, function(x) {
769+
mappings <- c(x$mapping, if (isTRUE(x$inherit.aes)) plot$mapping)
770+
if (originalData) {
771+
lapply(mappings, lazyeval::f_new)
772+
} else {
773+
nms <- names(mappings)
774+
setNames(lapply(nms, function(x) lazyeval::f_new(as.symbol(x))), nms)
775+
}
776+
})
777+
#if (!is.null(mappingFormulas[["group"]])) {
778+
# dat <- dplyr::group_by_(dat, mappingFormulas[["group"]])
779+
#}
773780
# don't need to add group as an attribute anymore
774-
mappingFormulas <- mappingFormulas[!grepl("^group$", names(mappingFormulas))]
775-
l$attrs <- setNames(list(mappingFormulas), l$cur_data)
776-
l$visdat <- setNames(list(function() dat), l$cur_data)
781+
# mappingFormulas <- mappingFormulas[!grepl("^group$", names(mappingFormulas))]
782+
l$attrs <- setNames(mappingFormulas, ids)
783+
l$attrs <- lapply(l$attrs, function(x) structure(x, class = "plotly_eval"))
784+
# the build step remove the first attrs if no type exists
785+
l$attrs[[1]][["type"]] <- "ggplotly"
786+
777787
l
778788
}
779789

R/plotly_build.R

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,18 +72,22 @@ plotly_build.plotly <- function(p) {
7272
p$x$layout$annotations <- annotations
7373

7474

75+
76+
7577
# If type was not specified in plot_ly(), it doesn't create a trace unless
7678
# there are no other traces
77-
if (is.null(p$x$attrs[[1]][["type"]])) {
78-
if (length(p$x$attrs) > 1 || isTRUE(attr(p, "ggplotly"))) {
79-
p$x$attrs[[1]] <- NULL
80-
}
79+
if (is.null(p$x$attrs[[1]][["type"]]) && length(p$x$attrs) > 1) {
80+
p$x$attrs[[1]] <- NULL
8181
}
8282

83+
# have the attributes already been evaluated?
84+
is.evaled <- function(x) inherits(x, "plotly_eval")
85+
attrsToEval <- p$x$attrs[!vapply(p$x$attrs, is.evaled, logical(1))]
86+
8387
# trace type checking and renaming for plot objects
8488
if (is_mapbox(p) || is_geo(p)) {
8589
p <- geo2cartesian(p)
86-
p$x$attrs <- lapply(p$x$attrs, function(tr) {
90+
attrsToEval <- lapply(attrsToEval, function(tr) {
8791
if (!grepl("scatter|choropleth", tr[["type"]] %||% "scatter")) {
8892
stop("Cant add a '", tr[["type"]], "' trace to a map object", call. = FALSE)
8993
}
@@ -218,7 +222,9 @@ plotly_build.plotly <- function(p) {
218222
trace[c("ymin", "ymax", "yend", "xend")] <- NULL
219223
trace[lengths(trace) > 0]
220224

221-
}, p$x$attrs, names2(p$x$attrs))
225+
}, attrsToEval, names2(attrsToEval))
226+
227+
p$x$attrs <- lapply(p$x$attrs, function(x) structure(x, class = "plotly_eval"))
222228

223229
# traceify by the interaction of discrete variables
224230
traces <- list()

0 commit comments

Comments
 (0)