title: Rendering Shiny user inputs based on conditional logic in server
body: <p> I'm trying to set up a navbar panel page in shiny where the user controls I display change based on the initial choice made in a set of radio buttons. I'm rendering the radio buttons directly in the ui and then building the conditional controls inside an "observed" logical control structure in Server.r. The error pops up because my initial if statement evaluates to false so the controls needed to filter my data don't get rendered and thus dplyr::filter() doesn't get what is expected. The thing I'm trying to figure out is that if I run this the radio buttons render with all other well panels blank. Then if I click on a few radio buttons the other ui controls appear eventually and then by clicking back on the "Regions" button the charts are called. How can I get the first if statement to see that "Regions" is selected? Or is there a way to get the radio buttons to update once the observer begins? At any rate any insights would be much appreciated. </p>
  
  <p><p><b> Error: </b><p></p>
  
  <pre><code> Listening on http://...............
    Error in eval(substitute(expr), envir, enclos) : 
    incorrect length (0), expecting: 192 
  </code></pre>
  
  <p><b> Global.R: </b></p>
  
  <pre><code>##### Play data
      options(stringsAsFactors = FALSE)
  
      exp &lt;- data.frame(scenario=rep(c(1,2,3,4),each=48),
                  region=rep(c("Western","Central","Southern","Northern","Capital","Hudson"),
                  times=4,each=8),
                  period=rep(c(1,2,3,4),times=48),
                  price=rep(c("Hi","Lo"),times=24,each=4),
                  val=rnorm(192,5,1.5))
  
  ##### Functions
  
      all_values &lt;- function(x) {
      if(is.null(x)) return(NULL)
      unique(x[,1])
      }
  </code></pre>
  
  <p><b> Server.R </b></p>
  
  <pre><code>
  
  library(shiny)
  library(ggvis)
  library(plyr)
  library(dplyr)
  
  shinyServer(function(input, output, session) {
  
  observe({
  
  if(input$comp=="Regions") { # For comparing regions #######################################
  
  ###### Define Controls ####### 
  
  output$regionO &lt;- renderUI({ checkboxGroupInput(inputId="regionI", label = h4("Regions"), 
          choices = list("Western", "Central", "Southern","Northern", "Capital", "Hudson"),
          selected = "Capital")
  }) 
  
  output$scenarioO &lt;- renderUI({ selectInput(inputId="scenarioI", label = h4("Scenario"), 
          choices = list("L1" = 1, "L2" = 2, "L3" = 3,"L4" = 4), 
          selected = 1)
  })
  
  output$fuelO &lt; renderUI({ selectInput(inputId="fuelI", label = h4("Fuel Price"), 
          choices = list("High Price" = "Hi", "Low Price" = "Lo"), 
          selected = "Lo")
  })
  
  ###### Define Data ########### 
  
  
  choose &lt;- reactive({ 
  
          chooseD &lt;- exp %>% filter(scenario == input$scenarioI, region %in% input$regionI, price == input$fuelI)
          names(chooseD)&lt;-c("scenario","NYregion","period","price","val")
          return(chooseD)
  })        
  
  
  sPlot1 &lt;- reactive({
  
          choose %>% ggvis(~factor(period),~val,stroke=~NYregion) %>% 
          layer_lines(strokeWidth:=2.5,strokeWidth.hover:=5) %>% 
          add_axis("x",title="Analysis Period") %>% add_tooltip(all_values,"hover") 
  }) 
  
  sPlot1 %>% bind_shiny("plot1") 
  sPlot1 %>% bind_shiny("plot2")  
  
  
  } 
  
  }) 
  
  # The code below is commented out so I can test the first condition of input$comp
  
  #else if(data==2) { # For comparing scenarios ######
  
  ###### Define Controls ####### 
  
  ###### Define Data ########### 
  
  #} else { # For comparing fuel prices ##############
  
  ###### Define Controls ####### 
  
  ###### Define Data ########### 
  
  #}
  
  }) # End Shiny Server
  
  </code></pre>
  
  <p><p><b> Ui.R: </b>
  
  <pre><code>
  library(shiny)
  library(ggvis)
  library(plyr)
  library(dplyr)
  
  shinyUI(navbarPage("TBR Economic Results Viewer", # theme="bootstrapCR.css",
  
    tabPanel("Summary Results",
     fluidRow(
        column(2,
         wellPanel(
          radioButtons(inputId="comp", label = h4("Comparison"),
          choices = list("Regions", "Scenarios", "Prices"), 
          selected ="Regions")
          ),
  
         wellPanel(
          uiOutput("regionO"),
          br(),
          uiOutput("scenarioO"),
          br(),
          uiOutput("fuelO")
          )
        ),
  
       column(5, 
        wellPanel(
          ggvisOutput("plot1"),
          textOutput("test")
          )
        ),
       column(5,
        wellPanel(
          ggvisOutput("plot2")
          ) 
        )
      )
   ),
  
   tabPanel("Detailed Results",
        mainPanel(
          plotOutput("plot2")
        )
      )
   )
  )
  </code></pre>