2

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))) to remove however many groups my model had created in a previous run.

What I haven't been able to figure out however, is how to add multiple traces, 1 for each factor in the target column, and color them by the colors in THECOLORS

library("shiny")
library("plotly")

rock[,2] <- sample(c('A', 'B', 'C'), 48, replace = T)
THECOLORS <- c('#383838', '#5b195b','#1A237E', '#000080', '#224D17', '#cccc00', '#b37400',  '#990000')

ui <- fluidPage(
  selectInput("dataset", "Choose a dataset:", choices = c("mtcars","rock")),

  plotlyOutput("Plot1")
)


server <- function(input, output, session) {


  dataSource <- reactive({switch(input$dataset,"rock" = rock,"mtcars" = mtcars)})

  output$Plot1 <-  renderPlotly({plot_ly(mtcars, x = ~mpg, y = ~hp, type = 'scatter', mode = 'markers', color = as.factor(mtcars$cyl), colors = THECOLORS) })

  observeEvent(input$dataset, {
    f <- list(
      family = "Courier New, monospace",
      size = 18,
      color = "#7f7f7f"
    )
    x <- list(
      title = "x Axis",
      titlefont = f, 
      range = c(0,(max(dataSource()[,1])+ 0.1*max(dataSource()[,1])))
    )
    y <- list(
      title = "y Axis",
      titlefont = f,
      range = c(0,(max(dataSource()[,4])+ 0.1*max(dataSource()[,4])))
    )
    plotlyProxy("Plot1", session) %>%
      plotlyProxyInvoke("deleteTraces",array(0:2)) %>% 
      # lapply(unique(dataSource()[,2], function(x) {  data <- dataSource()[which(dataSource()[,2] == x)],
      #                                   plotlyProxyInvoke("addTraces", 
      #                                     
      #                                     x = data()[,1],
      #                                     y = data()[,4],
      #                                     type = 'scatter',
      #                                     mode = 'markers')}) %>%

      plotlyProxyInvoke("relayout", list(xaxis = x, yaxis = y))
  })
}

shinyApp(ui, server)

1 Answer 1

3

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.