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)