title: Subset data based on input and create a plots with this data on Rshiny
body: <p>Roman history fan here, so I have a dataframe with the name of two legions (<code>fifth</code> and <code>tirteenth</code>), their casualties (numerical value), and the moral of the troops (<code>high</code>, <code>medium</code>, <code>low</code>).</p>
  <p>I want to know (boxplot) the relationship between moral (x axis) and casualties (y axis), and also subset by legion:</p>
  <pre><code>Legion &lt;- c(&quot;Fifth&quot;, &quot;Fifth&quot;, &quot;Fifth&quot;,&quot;Fifth&quot;,&quot;Fifth&quot;,&quot;Tirteenth&quot;,&quot;Tirteenth&quot;, &quot;Tirteenth&quot;, &quot;Tirteenth&quot;,&quot;Tirteenth&quot;)
  Casualties &lt;- c(13, 34,23,123,0,234,3,67,87,4)
  Moral &lt;- c(&quot;High&quot;, &quot;Medium&quot;, &quot;Low&quot;,&quot;High&quot;, &quot;Medium&quot;, &quot;Low&quot;,&quot;High&quot;, &quot;Medium&quot;, &quot;Low&quot;, &quot;High&quot;)
  romans &lt;- data.frame(Legion, Casualties, Moral)
  </code></pre>
  <p>Please notice that this is a toy example. In the real data (no romans) we have several variables for each of the axis, so we ask the user to load the data, and then select which variables he wants to use for each axis.</p>
  <p>This is what I have:</p>
  <pre><code>library(shiny)
  library(shinythemes)
  library(dplyr)
  library(readxl)
  library(ggplot2)
  
  not_sel &lt;- &quot;Not Selected&quot;
  
  main_page &lt;- tabPanel(
    title = &quot;Romans&quot;,
    titlePanel(&quot;Romans&quot;),
    sidebarLayout(
      sidebarPanel(
        title = &quot;Inputs&quot;,
        fileInput(&quot;xlsx_input&quot;, &quot;Select XLSX file to import&quot;, accept = c(&quot;.xlsx&quot;)),
        selectInput(&quot;num_var_1&quot;, &quot;Variable X axis&quot;, choices = c(not_sel)),
        selectInput(&quot;num_var_2&quot;, &quot;Variable Y axis&quot;, choices = c(not_sel)),
        selectInput(&quot;factor&quot;, &quot;Select factor&quot;, choices = c(not_sel)),
        br(),
        actionButton(&quot;run_button&quot;, &quot;Run Analysis&quot;, icon = icon(&quot;play&quot;))
      ),
      mainPanel(
        tabsetPanel(
          tabPanel(
            title = &quot;Plot&quot;,
            plotOutput(&quot;plot_1&quot;)
          )
        )
      )
    )
  )
  
  draw_plot_1 &lt;- function(data_input, num_var_1, num_var_2, factor){
  
    
    if(num_var_1 != not_sel &amp; num_var_2 != not_sel &amp; factor == not_sel){
      ggplot(data = data_input, aes_string(x = num_var_1, y = num_var_2, fill= num_var_2)) +
        geom_boxplot() + 
        theme_bw()
    }
    
    else if(num_var_1 != not_sel &amp; num_var_2 != not_sel &amp; factor != not_sel){
      ggplot(data = data_input, aes_string(x = num_var_1, y = num_var_2, fill = factor)) +
        geom_boxplot() + 
        theme_bw()
    }
  }
  
  
  ui &lt;- navbarPage(
    title = &quot;Plotter&quot;,
    theme = shinytheme(&quot;yeti&quot;),
    main_page
  )
  
  server &lt;- function(input, output){
    options(shiny.maxRequestSize=10*1024^2)
    
    data_input &lt;- reactive({
      req(input$xlsx_input)
      inFile &lt;- input$xlsx_input
      read_excel(inFile$datapath, 1)
    })
    
    
    observeEvent(data_input(),{
      choices &lt;- c(not_sel, names(data_input()))
      updateSelectInput(inputId = &quot;num_var_1&quot;, choices = choices)
      updateSelectInput(inputId = &quot;num_var_2&quot;, choices = choices)
      updateSelectInput(inputId = &quot;factor&quot;, choices = choices)
    })
    
    num_var_1 &lt;- eventReactive(input$run_button, input$num_var_1)
    num_var_2 &lt;- eventReactive(input$run_button, input$num_var_2)
    factor &lt;- eventReactive(input$run_button, input$factor)
    
      plot_1 &lt;- eventReactive(input$run_button,{
      draw_plot_1(data_input(), num_var_1(), num_var_2(), factor())
    })
    
    
    output$plot_1 &lt;- renderPlot(plot_1())
     
  }
  
  shinyApp(ui = ui, server = server)
  </code></pre>
  <p>I've been trying different methods to:</p>
  <ul>
  <li>First, let the user select which legion wants to plot.</li>
  <li>Implement this selection in plot.</li>
  </ul>
  <p>So far, the plot looks like this:</p>
  <p><a href="https://i.stack.imgur.com/KJaIR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KJaIR.png" alt="Boxplot" /></a>
  Any help given would be really appreciate it.</p>