A have starting complex initial_plot which is given by:
initial_plot <- plot_ly(y = as.numeric(substr(povrsina$x, 3, 8)),
x = as.numeric(substr(povrsina$y, 3, 8)),
z = povrsina$z, type = "surface", contours = list(x = list(highlight=F),
y = list(highlight=F),
z = list(highlight=F))) %>% layout(scene = list(xaxis = list(showspikes=FALSE),
yaxis = list(showspikes=F),
zaxis = list(showspikes=F,
range = c(min(povrsina$z), max(povrsina$z)+30))))
# colours <- RColorBrewer::brewer.pal(1, "Dark2")
initial_plot <- initial_plot %>% add_trace(y = Debla1$vb[1, ], x=Debla1$vb[2, ], z=Debla1$vb[3, ],
j = Debla1$ib[1, ]-1, i=Debla1$ib[2, ]-1, k=Debla1$ib[4, ]-1,
type = 'mesh3d',
fill = "dimgrey",
intensity = 1,
colorscale = list(c(0,'dimgrey'),
c(0.33,'dimgrey'),
c(0.66, 'dimgrey'),
c(1, 'dimgrey')),
hoverinfo = "none", showscale=F)
initial_plot <- initial_plot %>% add_trace(y=Krosnje1$vb[1, ], x=Krosnje1$vb[2, ], z=Krosnje1$vb[3, ],
i=c(Krosnje1$ib[1, ]-1, Krosnje1$ib[1, ]-1, Krosnje1$it[1, ]-1),
j=c(Krosnje1$ib[2, ]-1, Krosnje1$ib[3, ]-1, Krosnje1$it[2, ]-1),
k=c(Krosnje1$ib[3, ]-1, Krosnje1$ib[4, ]-1, Krosnje1$it[3, ]-1),
type='mesh3d',
intensity = 1,
colorscale = list(c(0,'forestgreen'),
c(0.33,'forestgreen'),
c(0.66, 'forestgreen'),
c(1, 'forestgreen')),
hoverinfo = "none", showscale=F)
Which gives a surface with two traces of positioned mesh3d objects, traces 0, 1, 2. Next, my mesh3d objects becoming larger in a loop triggered by an action button. In every iteration, smaller ones need to be replaced by newly created, bigger mesh3d objects. Here is the code:
server <- function(input, output, session) {
output$myPlot <- renderPlotly({
initial_plot
})
observeEvent(input$pokreniSimulaciju, {
for(i in 2:10){
# invalidateLater(1000)
plotlyProxy("myPlot", session) %>% plotlyProxyInvoke("addTraces", list(type='mesh3d',
y = get(paste0("Debla", i))$vb[1, ],
x = get(paste0("Debla", i))$vb[2, ],
z = get(paste0("Debla", i))$vb[3, ],
j = get(paste0("Debla", i))$ib[1, ]-1,
i = get(paste0("Debla", i))$ib[2, ]-1 ))
plotlyProxy("myPlot", session) %>% plotlyProxyInvoke("deleteTraces", list(as.integer(-1)))
plotlyProxy("myPlot", session) %>% plotlyProxyInvoke(method ="addTraces", list(type='mesh3d',
inherit = T, color = "forestgreen",
y = get(paste0("Krosnje", i))$vb[1, ],
x = get(paste0("Krosnje", i))$vb[2, ],
z = get(paste0("Krosnje", i))$vb[3, ],
i = c(get(paste0("Krosnje", i))$ib[1, ]-1, get(paste0("Krosnje", i))$ib[1, ]-1, get(paste0("Krosnje", i))$it[1, ]-1),
j = c(get(paste0("Krosnje", i))$ib[2, ]-1, get(paste0("Krosnje", i))$ib[3, ]-1, get(paste0("Krosnje", i))$it[2, ]-1),
k = c(get(paste0("Krosnje", i))$ib[3, ]-1, get(paste0("Krosnje", i))$ib[4, ]-1, get(paste0("Krosnje", i))$it[3, ]-1))
)
plotlyProxy("myPlot", session) %>% plotlyProxyInvoke("deleteTraces", list(as.integer(-2)))
}
})
}
But, after deleting the second one, my second mesh3d object changes the colour. Unfortunately, I couldn’t find a proper way to prevent this from happening. Any hint, thoughts about how to achieve this?