title: Shiny: use server output for further input to create plot
body: <p>I will have more than 2 datasets with various columns. The datasets differ by how frequently the data are collected. The data types/columns may be the same, but some will differ among datasets. I want to be able to create one plot at a time with data. I have code for selecting the dataset by collection frequency and for selecting the column/data type. I also have code for selecting the time window that a user may be interested in, for example, an hour, several hours, etc. I can not, though, figure out how to use the input from the time window to come up with the indices that are needed to select the appropriate rows from the selected column. Here is a sample of data (I also can't figure out how to make the following code wrap):</p>
  
  <pre><code>structure(list(`TIMESTAMP-TS` = structure(c(1432052400, 1432052700, 1432053000, 1432053300, 1432053600, 1432053900), class = c("POSIXct", "POSIXt"), tzone = ""), `RECORD-RN` = 49178:49183, `Rain_in_Tot-inch` = c(0, 0, 0, 0, 0, 0), `Stage-Feet` = c(-0.03, -0.03, -0.03, -0.03, -0.03, -0.03), `ISCO_Tot-` = c(0, 0, 0, 0, 0, 0)), .Names = c("TIMESTAMP-TS", "RECORD-RN", "Rain_in_Tot-inch", "Stage-Feet", "ISCO_Tot-"), row.names = c(NA, 6L), class = "data.frame")
  </code></pre>
  
  <p>Here is the code I have so far. </p>
  
  <h1>ui.R</h1>
  
  <pre><code>library(shiny)
  library(data.table)
  
  shinyUI(fluidPage(
  
  fluidRow(
  column(4, wellPanel(
  
        helpText("To begin, select the collection frequency you want to 
             view from the dropdown menu. Only one data type at a time is available."),
  
        selectInput("freqtype", "Collection frequency", choices = c("Five second", 
                "One minute", "Five minute", "Hourly"), size =4, selectize = FALSE),
  
        uiOutput("dynamicUI1"),
  
        helpText("Select dates"),
        uiOutput("dynamicUI2"),
  
        uiOutput("dynamicUI3"),
  
        textOutput("s2")
  
  )),
  
  fluidRow(
    wellPanel(
        plotOutput("plot")))
  
  )
  ))
  
  shinyServer(
  </code></pre>
  
  <h1>server.R</h1>
  
  <pre><code>function(input, output, session) {
      Inputfreq &lt;- reactive ({
            switch (input$freqtype, "Five second" = names(data.5sec), "One minute" = names(data.min), 
                   "Five minute" = names(data.5min), "Hourly" = names(data.hr))
          })
  
      Inputfreq2 &lt;&lt;- reactive ({
            switch (input$freqtype, "Five second" = data.5sec, "One minute" = data.min, "Five minute" = data.5min,
                    "Hourly" = data.hr)
          })
  
      output$dynamicUI1 &lt;- renderUI ({
            selectInput ("type", "Select data type", choices = c(Inputfreq2()[-1]))
          })
  
      output$dynamicUI2 &lt;- renderUI ({
            selectInput ("start", "Start date", choices = (as.character(as.POSIXct(Inputfreq2()$TIMESTAMP_TS))))
          })
  
      output$dynamicUI3 &lt;- renderUI ({
            selectInput ("finish", "Finish date", choices = c(as.character(as.POSIXct(Inputfreq2()$TIMESTAMP_TS))))
          })
  
      output$s2 &lt;- renderText({  #This is here to see what the value was for input$finish
        paste('tim.st =', input$finish)
          })
  
      output$tim.st &lt;- renderText({ #And here is where things get wonky.
        which(as.character(Inputfreq2()$TIMESTAMP_TS) == input$start)
          })
  
      output$tim.fi &lt;- observe ({ #Tried something different when I couldn't use renderText
        which(as.character(Inputfreq2()$TIMESTAMP_TS) == input$finish)
          })
  
      plotOutput(outputId = c(Inputfreq2()$TIMESTAMP_TS[which(Inputfreq2()$TIMESTAMP_TS == input$start:input$finish)], input$type[which(Inputfreq2()$TIMESTAMP_TS == input$start:input$finish)] ))  
  
          })
    }
  )
  </code></pre>
  
  <p>My thinking was to take the dataset selection as the first input, since that determines the data column/type and the frequency for the time window. I want to get the data column and time window inputs via the ui, but don't know how get the ui to refer back to the output for the selected dataset in the server. If I try in the ui</p>
  
  <pre><code>selectInput("fun", "func", choices = c(names(input$freqtype))),
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>ERROR: object 'input' not found
  </code></pre>
  
  <p>If I try </p>
  
  <pre><code>selectInput("fun", "func", choices = c(Inputfreq2)
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>ERROR: Operation not allowed without an active reactive context. (You 
  tried to do something that can only be done from inside a reactive 
  expression or observer.
  </code></pre>
  
  <p>So I go with <code>renderUI()</code> , but I don't seem to be using the output from that function correctly. As the code is here, when I run it, the UI displays the selection boxes for my inputs, but the plot apparently gets a NAN and gives a message indicating that or something similar, depending on how I tweak it. </p>
  
  <p><a href="https://stackoverflow.com/questions/19809824/shiny-use-results-of-function-call-in-observe-in-output">This question</a> seems kind of similar to mine and mentions something about putting one of the responses back into "global" and then using it from there, but I don't know what that means. </p>