Skip to content

Commit fb16ac0

Browse files
committed
Store group mappings as data attributes
1 parent dc4c6cd commit fb16ac0

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

R/add.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ add_trace <- function(p, ...,
6464

6565
p <- add_data(p, data)
6666

67-
# inherit attributes from the "first layer"
67+
# inherit attributes from the "first layer" (except the plotly_eval class)
6868
if (inherit) {
69-
attrs <- modify_list(p$x$attrs[[1]], attrs)
69+
attrs <- modify_list(unclass(p$x$attrs[[1]]), attrs)
7070
}
7171

7272
p$x$attrs <- c(

R/ggplotly.R

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -759,12 +759,7 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
759759
l <- rm_asis(l)
760760

761761
# 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
762+
# first, translate layer mappings -> plotly attrs
768763
mappingFormulas <- lapply(layers, function(x) {
769764
mappings <- c(x$mapping, if (isTRUE(x$inherit.aes)) plot$mapping)
770765
if (originalData) {
@@ -774,16 +769,27 @@ gg2list <- function(p, width = NULL, height = NULL, tooltip = "all",
774769
setNames(lapply(nms, function(x) lazyeval::f_new(as.symbol(x))), nms)
775770
}
776771
})
777-
#if (!is.null(mappingFormulas[["group"]])) {
778-
# dat <- dplyr::group_by_(dat, mappingFormulas[["group"]])
779-
#}
772+
773+
return_dat <- if (originalData) layer_data else data
774+
775+
# translate group aesthetics to data attributes
776+
return_dat <- Map(function(x, y) {
777+
if (is.null(y[["group"]])) return(x)
778+
dplyr::group_by_(x, y[["group"]])
779+
}, return_dat, mappingFormulas)
780+
780781
# don't need to add group as an attribute anymore
781-
# mappingFormulas <- mappingFormulas[!grepl("^group$", names(mappingFormulas))]
782+
mappingFormulas <- lapply(mappingFormulas, function(x) x[!grepl("^group$", names(x))])
783+
784+
ids <- lapply(seq_along(data), function(x) new_id())
782785
l$attrs <- setNames(mappingFormulas, ids)
783786
l$attrs <- lapply(l$attrs, function(x) structure(x, class = "plotly_eval"))
784-
# the build step remove the first attrs if no type exists
787+
# the build step removes the first attrs if no type exists
785788
l$attrs[[1]][["type"]] <- "ggplotly"
786789

790+
l$cur_data <- ids[[layerData]]
791+
l$visdat <- setNames(lapply(return_dat, function(x) function(y) x), ids)
792+
787793
l
788794
}
789795

0 commit comments

Comments
 (0)