title: Add plotly traces dynamically on shiny
body: <p>I am building an app that allows a user to dynamically add and remove traces on a plotly graph using selectInput.</p>
  
  <p>I have tried to play around with plotlyProxy () and plotlyProxyInvoke () from plotly package to no avail.</p>
  
  <p>Below is my rudimental code :</p>
  
  <pre><code>  library(shiny)
    library(shinydashboard)
    library(plotly)
  
  
      ui &lt;- dashboardPage(
         dashboardHeader(),
         dashboardSidebar(
         sidebarMenu(
          menuItem("Search", tabName = "Tabs", icon = icon("object-ungroup"))
  
          )
         ),
        dashboardBody(
        tabItem(tabName = "Tabs",
               fluidRow(
               column(width=3, 
                   box(
                     title="SELECT ",
                     solidHeader=TRUE,
                     collapsible=TRUE,
                     width=NULL,
                     selectInput(
                       inputId="Player",
                       selected = NULL, multiple = TRUE,
                       label=" Choose Player", 
                       choices=c("Messi", "Suarez", "Ronaldo" )),
                     selectInput(
                       inputId="Delete",
                       selected = NULL, multiple = TRUE,
                       label=" Choose Player", 
                       choices=c("Messi", "Suarez", "Ronaldo" )),
                     submitButton("Select")
                   )
            ),
  
            column( width=9,
                    tabBox(
                      width="100%",
                      tabPanel("tab1", 
                               plotlyOutput("Plot1")
                      )))))))
  
       server &lt;- function(input, output, session) {
             output$Plot1 &lt;-  renderPlotly({
  
            goals &lt;- data.frame(Name = c("Messi", "Suarez", "Ronaldo", "Messi", "Suarez", "Ronaldo", "Messi", "Suarez", "Ronaldo" ), 
                      Number= c(47, 35, 40, 49, 32, 31, 51, 49, 44 ),
                      Year = c("2018","2018","2018", "2017", "2017", "2017", "2016","2016","2016")
     )  
  
           plot_ly(goals, x = ~Year, y = ~Number, type = 'scatter', mode = 'lines', color = ~input$Player )%&gt;% layout(showlegend = TRUE)%&gt;%
    layout(title = 'Number of goals')
   })
  
       # plotly.addTraces
     observeEvent(input$Player, {
         plotlyProxy("Plot1", session) %&gt;%
          plotlyProxyInvoke("addTraces", list(x = ~Year, 
                                        y = ~Number,
                                        type = 'scatter',
                                        mode = 'lines'))
           })
  
         # plotly.deleteTraces
         observeEvent(input$Delete, {
          plotlyProxy("Plot1", session) %&gt;%
          plotlyProxyInvoke("deleteTraces")
           })
         }
     shinyApp(ui, server)
  </code></pre>
  
  <p>Is there a way to use the <em>plotlyProxyInvoke()</em> dynamically to add and remove traces without having to hardcode the traces using <em>addTrace()</em> ?</p>