Skip to content

Commit d7c1150

Browse files
committed
don't carry over eval envir; better logic for handling multiple layouts
1 parent 98ef8cc commit d7c1150

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

R/plotly.R

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ layout <- function(p = last_plot(), ...,
170170
enclos = parent.frame()
171171
)
172172
p <- last_plot(p)
173-
p$layout <- c(p$layout, list(layout))
173+
p$layout <- c(p$layout, list(layout = layout))
174174
if (evaluate) p <- plotly_build(p)
175175
hash_plot(data, p)
176176
}
@@ -273,10 +273,12 @@ plotly_build <- function(l = last_plot()) {
273273
} else if (any(idx)) {
274274
c(data = c(l$data, l[idx]), l[!idx])
275275
} else l
276-
# carry over properties, if necessary
276+
# carry over properties, if necessary (but don't carry over evaluation envir)
277277
if (length(l$data) > 1 && isTRUE(l$data[[1]]$inherit)) {
278+
d <- l$data[[1]]
279+
d <- d[!names(d) %in% c("env", "enclos")]
278280
for (i in seq.int(2, length(l$data))) {
279-
l$data[[i]] <- modifyList(l$data[[i]], l$data[[1]])
281+
l$data[[i]] <- modifyList(l$data[[i]], d)
280282
}
281283
}
282284
# 'x' is the same as 'l', but with arguments evaluated
@@ -322,17 +324,10 @@ plotly_build <- function(l = last_plot()) {
322324
x$data <- c(x$data, list(d))
323325
}
324326
}
325-
# layout() tacks on an unnamed list element to potentially pre-existing
326-
# layout(s). Note that ggplotly() will return a named list
327-
# of length n >= 1 (so we need to carefully merge them ).
327+
# it's possible have nested layouts (e.g., plot_ly() %>% layout() %>% layout())
328328
nms <- names(l$layout)
329-
if (!is.null(nms) && any(idx <- nms %in% "")) {
330-
# TODO: does this always preserve the correct order to layouts?
331-
# (important since we use modifyList at a later point)
332-
l$layout <- c(list(l$layout[!idx]), l$layout[idx])
333-
} else {
334-
l$layout <- list(l$layout)
335-
}
329+
idx <- nms %in% "layout"
330+
l$layout <- c(list(l$layout[!idx]), setNames(l$layout[idx], NULL))
336331
for (i in seq_along(l$layout)) {
337332
x$layout[[i]] <- perform_eval(l$layout[[i]])
338333
}

0 commit comments

Comments
 (0)