Use plotlyProxy to add multiple traces when data changes

后端 未结 1 1413
清歌不尽
清歌不尽 2021-01-26 07:31

Thank to this question: SO-Q I have now understood how to remove traces. In this case, I simply remove 0:2, but I can change that to i.e. array(O:unique(factor(df$group)))

1条回答
  •  有刺的猬
    2021-01-26 08:03

    Basically when using plotlyProxy and than plotlyProxyInvoke with "addTraces", "addTraces" is used to add more traces. You have to create a list of lists and each inner list would contain the details of each trace. In your case with many traces to add maybe some of the functions from the purrr package could help in creating the list of lists defining the traces.

    Take a look at the code below. It is a very simplified example, adding only two traces but the lists of list approach is there. Regarding your comment about the speed maybe you could load data only when needed and partially if your app concept allows for that...

    The code:

        library("shiny")
        library("plotly")
        library(purrr)
    
        ui <- fluidPage(
                selectInput("dataset", "Choose a dataset:", choices = c("rock", "mtcars")),
    
                plotlyOutput("Plot1")
        )
    
    
        server <- function(input, output, session) {
    
    
    
                output$Plot1 <-  renderPlotly({plot_ly(data = rock, x = ~area, 
                                                       y =~peri, mode = 'markers', type = 'scatter')})
    
                observeEvent(input$dataset, {
                        if (input$dataset == "rock") {
                                f <- list(
                                        family = "Courier New, monospace",
                                        size = 18,
                                        color = "#7f7f7f"
                                )
                                x <- list(
                                        title = "Area",
                                        titlefont = f, 
                                        range = c(0, max(rock$area))
                                )
                                y <- list(
                                        title = "Peri/Perm",
                                        titlefont = f,
                                        range = c(0, max(rock$peri))
                                )    
                                plotlyProxyInvoke(plotlyProxy("Plot1", session), "addTraces", list(list( 
                                        x = rock$area,
                                        y = rock$peri,
                                        type = 'scatter',
                                        mode = 'markers',
                                        marker = list(size = 10,
                                                      color = 'rgba(255, 182, 193, .9)',
                                                      line = list(color = 'rgba(0, 255, 0, .3)',
                                                                  width = 2))
                                ),
                                list( 
                                        x = rock$area,
                                        y = rock$perm,
                                        type = 'scatter',
                                        mode = 'markers',
                                        marker = list(size = 10,
                                                      color = 'rgba(255, 182, 193, .9)',
                                                      line = list(color = 'rgba(152, 0, 0, .8)',
                                                                  width = 2))
                                ))
                                ) 
                                plotlyProxy("Plot1", session) %>%
                                        plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
                                        plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
                        } else {
                                f <- list(
                                        family = "Courier New, monospace",
                                        size = 18,
                                        color = "#7f7f7f"
                                )
                                x <- list(
                                        title = "hp",
                                        titlefont = f, 
                                        range = c(0, max(mtcars$hp))
                                )
                                y <- list(
                                        title = "mpg/cyl",
                                        titlefont = f,
                                        range = c(0, max(mtcars$mpg))
                                ) 
                                plotlyProxyInvoke(plotlyProxy("Plot1", session), "addTraces", list(list( 
                                        x = mtcars$hp,
                                        y = mtcars$mpg,
                                        type = 'scatter',
                                        mode = 'markers',
                                        marker = list(size = 10,
                                                      color = 'rgba(255, 182, 193, .9)',
                                                      line = list(color = 'rgba(0, 255, 0, .3)',
                                                                  width = 2))
                                ),
                                list( 
                                        x = mtcars$hp,
                                        y = mtcars$cyl,
                                        type = 'scatter',
                                        mode = 'markers',
                                        marker = list(size = 10,
                                                      color = 'rgba(255, 182, 193, .9)',
                                                      line = list(color = 'rgba(152, 0, 0, .8)',
                                                                  width = 2))
                                ))
                                )   
                                plotlyProxy("Plot1", session) %>%
                                        plotlyProxyInvoke("deleteTraces", list(as.integer(0))) %>%
                                        plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
                        }
                })
        }
    
        shinyApp(ui, server)
    

    0 讨论(0)
提交回复
热议问题