title: R shiny app with inputs depending on updated data
body: <p>I am building a web app using shiny, and I'm unsure how to best structure the app since inputs depend on data and the outputs (charts) depend on aggregated data based on inputs. </p>
  
  <p>I tried to come up with a simple application to reproduce the problem. My setup is more advanced and unrelated to the example. Suppose you have a product line and want to analyse sales. Suppose that a data set is created for each day (I'm not saying the data structure is optimal, but it is useful for illustrating my question). Now in the application, one selects a date from a list of available dates, and then one selects a product. The dates are restricted to the period for which data is available, and the product list is restricted to products that were actually sold on the selected day. Then, we wish to plot the total sales value for each hour during the day.</p>
  
  <p>I will list some code for such an example below, where some sample data is also created. Sorry for the "long" code. It is sort of working, but I have some concerns.</p>
  
  <p>My questions are:</p>
  
  <p>1) I am wondering in which order things are executed, in particular when the app is first loaded, and then every time an input changes. Again, data depends on first input, the second input depends on the data. Third, a chart-friendly data set is computed which is used for the graph. You may notice that errors are printed to the console (and flashes briefly in the browser) but as the values are available, updates are made and the plot shows. It seems suboptimal.</p>
  
  <p>2) What is the current best practice when the inputs depend on data/server.R? I saw this <a href="https://groups.google.com/forum/?fromgroups=#!topic/shiny-discuss/JGJx5A3Ge-A">https://groups.google.com/forum/?fromgroups=#!topic/shiny-discuss/JGJx5A3Ge-A</a> but it seems like this is not implemented, even thought the post is rather old.</p>
  
  <p>Here is the code for the two files:</p>
  
  <pre><code># ui.R
  ######
  
  library(shiny)
  
  shinyUI(pageWithSidebar(
  
    headerPanel("New Application"),
  
    sidebarPanel(
      htmlOutput("dateInput"),
      htmlOutput("prodInput")
    ),
  
    mainPanel(
      plotOutput("salesplot")  
    )
  
  ))
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>#server.R
  #########
  
  library(shiny)
  library(filehash)
  
  set.seed(1)
  
  dates    &lt;- format(seq(Sys.Date() - 10, Sys.Date(), "days"), "%Y-%m-%d")
  products &lt;- LETTERS
  prices   &lt;- sample(10:100, size = length(products), replace = TRUE)
  names(prices) &lt;- LETTERS
  
  if (file.exists("exampledb")) {
  
    db &lt;- dbInit("exampledb")
  
  } else {
  
    dbCreate("exampledb")
    db &lt;- dbInit("exampledb")
  
    for (d in dates) {
      no.sales &lt;- sample(50:100, size = 1)
      x &lt;- data.frame(
         product     = sample(products, size = no.sales, replace = TRUE)
        ,hour        = sample(8:20, size = no.sales, replace = TRUE) 
        ,order.size  = sample(1:10, size = no.sales, replace = TRUE)
      )
      x$price &lt;- prices[x$product]
      dbInsert(db, paste0("sales", gsub("-", "", d)), x)
    }
  }
  
  
  current &lt;- reactiveValues()
  
  shinyServer(function(input, output) {
  
    inputDates &lt;- reactive({ 
      sort(strptime(unique(substr(names(db), 6, 13)), "%Y%m%d")) 
    })
  
    output$dateInput &lt;-  renderUI({ dateInput(
      inputId    = "date",
      label      = "Choose hour",
      min        = min(inputDates()),
      max        = max(inputDates()), 
      format     = "yyyy-mm-dd", 
      startview  = "month",
      weekstart  = 0, 
      language   = "en")
    })
  
    inputProducts &lt;- reactive({
      current$data &lt;&lt;- dbFetch(db, paste0("sales", format(input$date, "%Y%m%d")))
      sort(unique(current$data$product))
    })
  
    output$prodInput &lt;- renderUI({ selectInput(
      inputId    = "product",
      label      = "Choose Product",
      choices    = inputProducts(),
      selected   = 1)
    })
  
    output$salesplot &lt;- renderPlot({
      pdata &lt;- aggregate(I(order.size*price) ~ hour,
        data = subset(current$data, product == input$product),
        FUN  = sum)
      colnames(pdata)[2] &lt;- "value"      
      plot(value ~ hour, data = pdata, xlim = c(8, 20))
    })
  
  
  })
  </code></pre>