title: Query on button press in Shiny app (R)
body: <p>I am making an interactive Shiny app that searches a MySQL database on my computer. I can access the database and properly execute queries in R scripts and static Shiny apps, but I run into trouble when trying to execute queries only when a user clicks on an action button. </p>
  
  <p>The problems occur when I wrap my queries in Shiny's event triggered functions, like <code>observeEvent()</code>. For example, the following code runs and retrieves the right set of data:</p>
  
  <pre><code>shinyServer(function(input, output, session){
      con &lt;- dbConnect(RMySQL::MySQL(), dbname = "testdb", user='testuser', password = "testpassword")
      tableList &lt;- dbListTables(con)
      output$out &lt;- renderPrint(tableList) #this always prints
  
      df &lt;- data.frame()
      #observeEvent(input$button, {
          quer &lt;- paste("select id, dept, course_code from main_course_info
                         where dept in (select id from dept_relations 
                                        where deptName = 'CMPE')")
          df &lt;- data.frame(dbGetQuery(con, quer))
          output$table &lt;- renderTable({df})
       #})
      dbDisconnect(con)
   })#shinyServer
  </code></pre>
  
  <p>With the <code>observeEvent</code> function commented out, the query runs perfectly and the retrieved table is displayed. But when I try to hold the query off until input$button changes, i.e the button gets pressed, i get the following error upon pressing the button:</p>
  
  <pre><code> Warning in run(timeoutMs) :
     Unhandled error in observer: internal error in RS_DBI_getConnection: corrupt connection handle
   observeEvent(input$button)
  </code></pre>
  
  <p>I need this button functionality to work, because my goal is to have the specifics of the query decided by user inputs, which would be brought together and executed once the user has made their selections and hit the action button. However I cannot even get a predefined query to run with <code>observeEvent</code>, which is integral to the design of my app.</p>
  
  <p>Note that the first output, a <code>renderPrint</code> of the list of tables in the database, always prints regardless of the success or failure of the button. This means that the connection is working at least until it gets sent to <code>observeEvent</code>'s handler argument.</p>