Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subplot(): avoid Reduce() when merging lists #1498

Merged
merged 3 commits into from
Jul 15, 2019

Conversation

alyst
Copy link
Contributor

@alyst alyst commented Mar 20, 2019

The PR replaces the uses of Reduce(c, ...) to merge the lists with unlist(..., recursive = FALSE).

The motivation: for the list x of n elements Reduce(c, x) would implicitly create n lists/vectors of lengths 1 to n, whereas only the last one is needed. The pattern adds a considerable overhead when the subplots contain a lot of data.

It also simplifies ensure_one(): instead of using [ to get the one-element list, it uses [[ to get this one element (nulls are automatically filtered out).

alyst added 2 commits March 20, 2019 10:34
use do.call() instead to avoid quadratic complexity of growing lists
@cpsievert
Copy link
Collaborator

cpsievert commented Apr 3, 2019

Thanks @alyst , this looks great! Do you have an example in mind where db622ad is relevant? (Update: Here's an example where db622ad gets rid of a superfluous warning)

subplot(
  plot_ly(x = 1, source = "A"),
  plot_ly(x = 1, source = NULL)
)

@cpsievert cpsievert requested a review from jcheng5 April 4, 2019 21:52
p$attrs <- Reduce(c, lapply(plots, "[[", "attrs"))
p$layout <- modify_list(p$layout, Reduce(modify_list, layouts))
p$attrs <- unlist(lapply(plots, "[[", "attrs"), recursive = FALSE)
p$layout <- Reduce(modify_list, layouts, p$layout)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are a lot of layouts then do.call(modify_list, c(list(p$layout), layouts)) might be better (or something like that with judicious quote() calls added--@wch can give you details if you choose to go the do.call route). If there aren't a lot of layouts then this is fine with me.

@cpsievert cpsievert merged commit 3bdb921 into plotly:master Jul 15, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants