title: How to get a subset of data using an input that's dependent on another input in shiny?
body: <p>I tried to add a filter to my data analysis. The filter (inputF2) is an item in a category (xInput) chosen by the user. </p>
  
  <p>then I want filter out the data to do summarize analysis and plot out the mean. However, once I wrote the if statement, the program won't run.  </p>
  
  <pre><code>library(datasets)
  library(shiny)
  library(dplyr)
  library(ggplot2)
  library(DT)
  library(crosstalk)
  
  data("iris")
  
  # Define UI for application that draws a histogram
  ui &lt;- fluidPage(
  
    # Application title
    titlePanel("Analyze Iris table"),
  
    # Sidebar with a dropdown menu selection input for key measurecomponent
    sidebarLayout(
      sidebarPanel(
        selectInput("yInput", "Measuring element: ", 
                    colnames(iris), selected = colnames(iris)[2]), 
        selectInput('xInput', 'Grouper: ', 
                    colnames(iris), selected = colnames(iris)[5])
      ),
  
      # Show a plot of the generated distribution
      mainPanel(
        uiOutput('filter'),
        plotOutput("barPlot"),
        DTOutput('table1')
        )))
  server &lt;- function(input, output) {
  
    output$filter = renderUI({
  
      selectInput('inputF2', 'Filter Item: ', 
                  c('Null', unique(iris %&gt;% select(input$xInput))))
    })
  
    if(input$inputF2 != 'Null') {
      iris_sub = reactive({
  
        iris %&gt;% filter_at(input$xInput == input$inputF2)
  
      })
    } else{ iris_sub = iris}
  
    by_xInput &lt;- reactive({
  
      iris_sub %&gt;% 
        group_by_at(input$xInput) %&gt;% 
        summarize(n = n(), mean_y = mean(!! rlang::sym(input$yInput)))
  
    })
  
    output$barPlot &lt;- renderPlot({
  
      # as the input is a string, use `aes_string`
      ggplot(data = by_xInput(), aes_string(x = input$xInput, y = "mean_y")) + 
        geom_bar(stat = 'identity')
  
    })
  
    output$table1 = renderDT(
      datatable(by_xInput())
      )
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  
  <p>This is the error message I got: </p>
  
  <blockquote>
    <p>Error in .getReactiveEnvironment()$currentContext() : 
      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.)</p>
  </blockquote>