title: Shiny reactive selectInput in R
body: <p>I have an application shiny containing reactive data. I would like the application to select only non-empty columns for the X and Y axes. At the moment I choose between <code>colnames(TD[,3:7])</code>, but there are also empty values for columns, so I don't want these columns to show up for the variable selection. Below is an example and my code:</p>
  
  <pre><code>type &lt;- as.character(c('summer','summer','summer','summer','winter','winter','winter','winter'))
  country &lt;- as.character(c('A','A','B','B','A','A','B','B'))
  year &lt;- c(2011,2012,2013,2014,2011,2012,2013,2014)
  col1 &lt;- c(33,7,NA,NA,5,11,NA,NA)
  col2 &lt;- c(10,3,NA,NA,8,15,NA,NA)
  col3 &lt;- c(NA,NA,10,15,NA,NA,20,25)
  col4 &lt;- c(NA,NA,8,5,NA,NA,22,16)
  
  TD &lt;- data.frame(type,country,year,col1,col2,col3,col4,stringsAsFactors=FALSE)
  
  library(readxl)
  library(shiny)
  library(ggplot2)
  library(shinythemes)
  library(DT)
  
  ui &lt;-shinyUI(fluidPage(pageWithSidebar(
    headerPanel("Test App"),
    sidebarPanel(
      selectInput("type","Choose a type", choices = c("All",unique(TD$type))),
      selectInput("country","Choose an country", choices = c("All",unique(TD$country))),
      selectInput("yaxis", "Choose a y variable", choices = colnames(TD[,3:7])),
      selectInput("xaxis", "Choose a x variable", choices = colnames(TD[,3:7])),
      actionButton("goButton", "Update")
    ),
    mainPanel(
      tabsetPanel(
        tabPanel('Plot', plotOutput("plot1"))
      ))
  )
  ))
  
  server &lt;- shinyServer(function(input,output, session){
  
    data1 &lt;- reactive({
      if(input$type == "All"){
        TD
      }
      else{
        TD[which(TD$type == input$type),]
      }
    })
    data2 &lt;- eventReactive(input$goButton,{
      if (input$country == "All"){
        TD
      }else{
  
        TD[which(TD$country == input$country),]
      }
    })
  
    observe({
      if(input$type != "All"){
        updateSelectInput(session,"country","Choose a country", choices = c("All",unique(data1()$country)))
      }
      else if(input$country != 'All'){
        updateSelectInput(session,"type","Choose a type", choices = c('All',unique(data2()$type)))
      }
      else if (input$type == "All" &amp; input$country == "All"){
        updateSelectInput(session,"country","Choose a country", choices = c('All',unique(TD$country)))
        updateSelectInput(session,"type","Choose a type", choices = c('All',unique(TD$type)))
      }
    })
  
    data3 &lt;- eventReactive( input$goButton,{
      req(input$goButton)
      req(input$goButton)
      if(input$country == "All"){
        data1()
      }
      else if (input$type == "All"){
        data2()
      }
      else if (input$country == "All" &amp; input$type == "All"){
        TD
      }
      else
      {
        TD[which(TD$country== input$country &amp; TD$type == input$type),]
      }
    })
  
    x_var&lt;- eventReactive(input$goButton, {
      input$xaxis
    })
    y_var &lt;- eventReactive(input$goButton,{
      input$yaxis
    })
  
    output$plot1 &lt;- renderPlot({
      x &lt;- x_var()
      y &lt;- y_var()
      p &lt;- ggplot(data3(),aes(x=data3()[,x], y=data3()[,y])) + geom_line() + geom_point()
      p + labs(x = x_var(), y = y_var()) + theme(plot.title = element_text(hjust = 0.5, size=20))
    })
  })
  
  shinyApp(ui,server)
  
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/39sAn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/39sAn.jpg" alt="enter image description here"></a></p>