Description
When producing a plot with both cumulative and non-cumulative animations on the same plot where I assign names to both traces, I get the error
Error in which(idx)[[1]] : subscript out of bounds
when trying to print the plot. I've debugged the issue and I think I isolated the problem, although I am not sure on how to fix it.
I believe the problem is here: https://github.com/ropensci/plotly/blob/9bc585ac0d689ad22e30e086ece6073081a9c37b/R/plotly_build.R#L484
I think traceNamesMissing
is supposed to hold the names of the traces that are missing on the particular frame d
, but present on other frames. However, when debugging, frameTraceNames
is a list of unique trace names but
sapply(d, "[[", "name")
produces a list of character vectors in my situation, and therefore traceNamesMissing
gets assigned with the name of all the traces of the plot. For my particular situation, I've been able to resolve the issue by replacing line 484 with
traceNamesMissing <- setdiff(frameTraceNames, unlist(sapply(d, "[[", "name")))
However, since I am not that familiar with the internal workings of the package I don't think this solution can work for all cases.
Here is my reprex.
suppressMessages(library(tidyverse))
suppressMessages(library(plotly))
df1<- tibble(frame = seq(1:10)) %>% mutate(x = row_number(), y = 0)
df2<- tibble(rowid = seq(1:10)) %>% mutate(x = row_number(), y = 1) %>%
split(.$rowid) %>%
accumulate(function(old, new){
bind_rows(old, new)
}) %>%
bind_rows(.id = "frame") %>%
mutate(frame = as.numeric(frame))
p_noNames<- plot_ly(df1, type = "scatter") %>%
add_trace(mode = "markers", x = ~x, y = ~y, frame = ~frame) %>%
add_trace(data = df2, mode = "lines", x = ~x, y = ~y, frame = ~frame)
out_noNames<- plotly_build(p_noNames)
#> No scatter mode specifed:
#> Setting the mode to markers
#> Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
p_withNames<- plot_ly(df1, type = "scatter") %>%
add_trace(mode = "markers", x = ~x, y = ~y, frame = ~frame, name= "A") %>%
add_trace(data = df2, mode = "lines", x = ~x, y = ~y, frame = ~frame, name = "B")
out_withNames<- plotly_build(p_withNames)
#> No scatter mode specifed:
#> Setting the mode to markers
#> Read more about this attribute -> https://plot.ly/r/reference/#scatter-mode
#> Error in which(idx)[[1]]: subscript out of bounds