In Shiny, update DataTable with new values from user input
<p>I'm writing a shiny app that has a table (using <code>DT::renderDataTable</code>) from which users can select a row. But I want the user to also be able to add new row(s) if what they want is not already in the table.  I'm using input controls for the user to enter new data, and I have an action button which, if pressed, should create a new row of data in the table from the input values. But pressing the button does not update the table.</p>

<p>A minimal example:</p>

<pre><code>library(shiny)
library(DT)
mydata = data.frame(id=letters[1:5], val=sample(10,5,T))

ui = fluidPage(dataTableOutput("table"),
               textInput('NewID', 'Enter new ID'),
               numericInput('NewVal', 'Enter new val', 1),
               actionButton("goButton", "Update Table"))

server = function(input,output){
  output$table = renderDataTable(mydata)
  update = eventReactive(input$goButton, {
    newrow = data.frame(id = input$NewID, val = input$NewVal)
    mydata = rbind(mydata, newrow)
  })
}

shinyApp(ui,server)
</code></pre>

<p>Clearly, this is the wrong way to approach this. I've tried various combinations of wrapping both <code>renderDataTable</code> and the code to update <code>mydata</code> inside <code>renderUI</code>, <code>observe</code> and <code>reactive</code>, but I can't find the right way to do this.</p>

<p>This is my first shiny app, so maybe there is a basic concept that I'm not quite grasping. What is the right way?</p>