adding points to plotly based on user input

Hi, I'm new to plotly and am hoping for help. I currently have a reactive plot that plots 1 point based on user input. Once the user input is changed, the graph is cleared and a new point is plotted. Instead, I would like to keep all points on the plot, and add additional points as the user selects them. Any tips?

          plot <- plot_ly(
            x = vals$Leachable,
            y = vals$Yield, 
            type = 'scatter',
            mode = 'markers')
          plot <- plot %>% layout(xaxis = x, yaxis = y)

In this case, both my x and y variable are reactive and depend on user input.


I always use plotlyProxy for updating a plotly plot. Then you can use addTrace() to add more points.

Initially render the plotly using renderPlotly as you have done.

To update the plotly instead you use plotlyProxy. This avoids drawing the chart from scratch. e.g.

output$plot <- renderPlotly({
     # your code
observeEvent(c(vals$Leachable, vals$Yield), {
   plotlyProxy("plot") %>%
        list(x = c(val$Leachable,val$Leachable), # need at least 2 points
              y = c(val$Yield,val$Yield),
              type = "scatter",
              mode = "markers"

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.