Skip to content

Commit 3bdb921

Browse files
authored
Merge pull request #1498 from alyst/avoid_reduce
subplot(): avoid Reduce() when merging lists
2 parents 2a439bc + 51a8099 commit 3bdb921

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

R/subplots.R

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
266266
}
267267

268268
p <- list(
269-
data = Reduce(c, traces),
269+
data = unlist(traces, recursive = FALSE),
270270
layout = Reduce(modify_list, c(xAxes, rev(yAxes)))
271271
)
272272

@@ -275,9 +275,10 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
275275
annotations <- Map(reposition, annotations, split(domainInfo, seq_along(plots)))
276276
shapes <- Map(reposition, shapes, split(domainInfo, seq_along(plots)))
277277
images <- Map(reposition, images, split(domainInfo, seq_along(plots)))
278-
p$layout$annotations <- Reduce(c, annotations)
279-
p$layout$shapes <- Reduce(c, shapes)
280-
p$layout$images <- Reduce(c, images)
278+
p$layout$annotations <- unlist(annotations, recursive = FALSE)
279+
p$layout$shapes <- unlist(shapes, recursive = FALSE)
280+
p$layout$images <- unlist(images, recursive = FALSE)
281+
281282
# merge non-axis layout stuff
282283
layouts <- lapply(layouts, function(x) {
283284
x[!grepl("^[x-y]axis|^geo|^mapbox|annotations|shapes|images", names(x))] %||% list()
@@ -290,8 +291,8 @@ subplot <- function(..., nrows = 1, widths = NULL, heights = NULL, margin = 0.02
290291
}
291292
layouts <- layouts[which_layout]
292293
}
293-
p$attrs <- Reduce(c, lapply(plots, "[[", "attrs"))
294-
p$layout <- modify_list(p$layout, Reduce(modify_list, layouts))
294+
p$attrs <- unlist(lapply(plots, "[[", "attrs"), recursive = FALSE)
295+
p$layout <- Reduce(modify_list, layouts, p$layout)
295296
p$source <- ensure_one(plots, "source")
296297
p$config <- ensure_one(plots, "config")
297298
p$highlight <- ensure_one(plots, "highlight")
@@ -338,14 +339,18 @@ dots2plots <- function(...) {
338339
# helper function that warns if more than one plot-level attribute
339340
# has been specified in a list of plots (and returning that attribute)
340341
ensure_one <- function(plots, attr) {
341-
attrs <- lapply(plots, "[", attr)
342+
attrs <- Filter(Negate(is.null), lapply(plots, "[[", attr))
343+
if (length(attrs) == 0) {
344+
warning("No ", attr, " found", call. = FALSE)
345+
return (NULL)
346+
}
342347
for (i in seq_along(attrs)) {
343348
if (!identical(attrs[[1]], attrs[[i]])) {
344349
warning("Can only have one: ", attr, call. = FALSE)
345350
break
346351
}
347352
}
348-
attrs[[length(attrs)]][[1]]
353+
attrs[[length(attrs)]]
349354
}
350355

351356

@@ -399,7 +404,7 @@ get_domains <- function(nplots = 1, nrows = 1, margins = 0.01,
399404

400405
list2df <- function(x, nms) {
401406
#stopifnot(length(unique(sapply(x, length))) == 1)
402-
m <- if (length(x) == 1) t(x[[1]]) else Reduce(rbind, x)
407+
m <- if (length(x) == 1) t(x[[1]]) else do.call(rbind, x)
403408
row.names(m) <- NULL
404409
df <- data.frame(m)
405410
if (!missing(nms)) setNames(df, nms) else df

0 commit comments

Comments
 (0)