title: R shiny passing reactive to selectInput choices
body: <p>In a shiny app (by RStudio), on the <em>server</em> side, I have a reactive that returns a list of variables by parsing the content of a <code>textInput</code>. The list of variables is then used in <code>selectInput</code> and/or <code>updateSelectInput</code>.</p>
  
  <p>I can't make it work. Any suggestions?</p>
  
  <p>I have made two attempts. The first approach is to use the reactive <code>outVar</code> directly into <code>selectInput</code>. The second approach is to use the reactive <code>outVar</code> in <code>updateSelectInput</code>. Neither works.</p>
  
  <h3>server.R</h3>
  
  <pre><code>shinyServer(
    function(input, output, session) {
  
      outVar &lt;- reactive({
          vars &lt;- all.vars(parse(text=input$inBody))
          vars &lt;- as.list(vars)
          return(vars)
      })
  
      output$inBody &lt;- renderUI({
          textInput(inputId = "inBody", label = h4("Enter a function:"), value = "a+b+c")
      })
  
      output$inVar &lt;- renderUI({  ## works but the choices are non-reactive
          selectInput(inputId = "inVar", label = h4("Select variables:"), choices =  list("a","b"))
      })
  
      observe({  ## doesn't work
          choices &lt;- outVar()
          updateSelectInput(session = session, inputId = "inVar", choices = choices)
      })
  
  })
  </code></pre>
  
  <h3>ui.R</h3>
  
  <pre><code>shinyUI(
    basicPage(
      uiOutput("inBody"),
      uiOutput("inVar")
    )
  )
  </code></pre>
  
  <p>A short while ago, I posted the same question at shiny-discuss, but it has generated little interest, so I'm asking again, with apologies, <a href="https://groups.google.com/forum/#!topic/shiny-discuss/e0MgmMskfWo" rel="noreferrer">https://groups.google.com/forum/#!topic/shiny-discuss/e0MgmMskfWo</a></p>
  
  <p><strong>Edit 1</strong></p>
  
  <p>@Ramnath has kindly posted a solution that appears to work, denoted <em>Edit 2</em> by him. But that solution does not address the problem because the <code>textinput</code> is on the <code>ui</code> side instead of on the <code>server</code> side as it is in my problem. If I move the <code>textinput</code> of Ramnath's second edit to the <code>server</code> side, the problem crops up again, namely: nothing shows and RStudio crashes. I found that wrapping <code>input$text</code> in <code>as.character</code> makes the problem disappear.</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>In further discussion, Ramnath has shown me that the problem arises when the server attempts to apply the dynamic function <code>outVar</code> before its arguments have been returned by <code>textinput</code>. The solution is to first check whether <code>is.null(input$inBody)</code> exists.</p>
  
  <p><em><strong>Checking for existence of arguments is a crucial aspect of building a shiny app</strong></em>, so why did I not think of it? Well, I did, but I must have done something wrong! Considering the amount of time I spent on the problem, it's a bitter experience. I show after the code how to check for existence.</p>
  
  <p>Below is Ramnath's code with <code>textinput</code> moved to the <code>server</code> side. It crashes RStudio so don't try it at home. (I have used his notation)</p>
  
  <pre><code>library(shiny)
  runApp(list(
    ui = bootstrapPage(
      uiOutput('textbox'),  ## moving Ramnath's textinput to the server side
      uiOutput('variables')
    ),
    server = function(input, output){
      outVar &lt;- reactive({
        vars &lt;- all.vars(parse(text = input$text))  ## existence check needed here to prevent a crash
        vars &lt;- as.list(vars)
        return(vars)
      })
  
      output$textbox = renderUI({
        textInput("text", "Enter Formula", "a=b+c")
      })
  
      output$variables = renderUI({
        selectInput('variables2', 'Variables', outVar())
      })
    }
  ))
  </code></pre>
  
  <p>The way I usually check for existence is like this:</p>
  
  <pre><code>if (is.null(input$text) || is.na(input$text)){
    return()
  } else {
    vars &lt;- all.vars(parse(text = input$text))
    return(vars)
  }
  </code></pre>
  
  <p>Ramnath's code is shorter:</p>
  
  <pre><code>if (!is.null(mytext)){
    mytext = input$text
    vars &lt;- all.vars(parse(text = mytext))
    return(vars)
  }
  </code></pre>
  
  <p>Both seem to work, but I'll be doing it Ramnath's way from now on: maybe an unbalanced bracket in my construct had earlier prevented me to make the check work? Ramnath's check is more direct.</p>
  
  <p>Lastly, I'd like to note a couple of things about my various attempts to debug. </p>
  
  <p>In my debugging quest, I discovered that there is an option to "rank" the priority of "outputs" on the server side, which I explored in an attempt to solve my problem, but didn't work since the problem was elsewhere. Still, it's interesting to know and seems not very well known at this time:</p>
  
  <pre><code>outputOptions(output, "textbox", priority = 1)
  outputOptions(output, "variables", priority = 2)
  </code></pre>
  
  <p>In that quest, I also <em>tried</em> <code>try</code>:</p>
  
  <pre><code>try(vars &lt;- all.vars(parse(text = input$text)))
  </code></pre>
  
  <p>That was pretty close, but still did not fix it.</p>
  
  <p>The first solution I stumbled upon was: </p>
  
  <pre><code>vars &lt;- all.vars(parse(text = as.character(input$text)))
  </code></pre>
  
  <p>I suppose it would be interesting to know why it worked: is it because it slows things down enough? is it because <code>as.character</code> "waits" for <code>input$text</code> to be non-null?</p>
  
  <p>Whatever the case may be, I am extremely grateful to Ramnath for his effort, patience and guidance.</p>