@@ -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
0 commit comments