title: R shiny variable graphs
body: <p>I have a dataframe with multiple data types. I want to change graph type whenever the input in the shiny app changes in the sidebar panel. The condition I am trying to satisfy are</p>
  <ol>
  <li>If 2 numerical variables are selected plot a scatter plot</li>
  <li>if one categorical and one numerical variable is selected plot a boxplot</li>
  <li>if two categorical variables are selected plot a barplot</li>
  </ol>
  <p>The code I have tried till now</p>
  <pre><code>library(shiny)
  library(ggplot2)
  library(readr)
  library(tidyverse)
  
  
  df  &lt;- read_table(&quot;cars.xls&quot;)
  
  ui &lt;- shinyUI(fluidPage(
    
    # Application title
    titlePanel(&quot;Car Dataset Analysis&quot;),
    
    # Sidebar with a slider input for number of bins
    sidebarLayout(
      sidebarPanel(
        selectInput(&quot;X_variable&quot;, &quot;X Variable:&quot;, 
                    c(&quot;No of Cylinders&quot; = &quot;Cylinders&quot;,
                      &quot;Type of car&quot; = &quot;Type&quot;,
                      &quot;Engine Size&quot; = &quot;EngineSize&quot;,
                      &quot;Fuel Tank capacity&quot; = &quot;Fuel.tank.capacity&quot;,
                      &quot;Origin of the car&quot;=&quot;Origin&quot;,
                      &quot;Prices&quot;=&quot;Price&quot;,
                      &quot;Rev /min&quot;=&quot;RPM&quot;,
                      &quot;Weight of car&quot;=&quot;Weight&quot;,
                      &quot;MPG city&quot;=&quot;MPG.city&quot;,
                      &quot;Horsepower of car&quot;=&quot;HorsePower&quot;,
                      &quot;Passengers capacity&quot;=&quot;Passengers&quot;,
                      &quot;Length of car&quot;=&quot;Length&quot;,
                      &quot;Manufacturer of car&quot;=&quot;Manufacturer&quot;,
                      &quot;Model of car&quot;=&quot;Model&quot; )),
        selectInput(&quot;Y_variable&quot;, &quot; Y Variable:&quot;, 
                    c(&quot;No of Cylinders&quot; = &quot;Cylinders&quot;,
                      &quot;Type of car&quot; = &quot;Type&quot;,
                      &quot;Engine Size&quot; = &quot;EngineSize&quot;,
                      &quot;Fuel Tank capacity&quot; = &quot;Fuel.tank.capacity&quot;,
                      &quot;Origin of the car&quot;=&quot;Origin&quot;,
                      &quot;Prices&quot;=&quot;Price&quot;,
                      &quot;Rev /min&quot;=&quot;RPM&quot;,
                      &quot;Weight of car&quot;=&quot;Weight&quot;,
                      &quot;MPG city&quot;=&quot;MPG.city&quot;,
                      &quot;Horsepower of car&quot;=&quot;HorsePower&quot;,
                      &quot;Passengers capacity&quot;=&quot;Passengers&quot;,
                      &quot;Length of car&quot;=&quot;Length&quot;,
                      &quot;Manufacturer of car&quot;=&quot;Manufacturer&quot;,
                      &quot;Model of car&quot;=&quot;Model&quot; )),
  
      mainPanel(
        tabsetPanel(type=&quot;tabs&quot;,
                    
        tabPanel(&quot;Plots&quot;,
          plotOutput(&quot;main_plot&quot;)),  
  
        )
      )
    )
  ))
  
  server &lt;- shinyServer(function(input, output) {
    
    data &lt;- df
    
    
    data$Model &lt;- factor(data$Model)
    data$Manufacturer &lt;- factor(data$Manufacturer)
    data$Origin &lt;- factor(data$Origin)
  data2 &lt;- data.frame(data)
    
    choices &lt;- c(&quot;No of Cylinders&quot; = &quot;Cylinders&quot;,
                 &quot;Type of car&quot; = &quot;Type&quot;,
                 &quot;Engine Size&quot; = &quot;EngineSize&quot;,
                 &quot;Fuel Tank capacity&quot; = &quot;Fuel.tank.capacity&quot;,
                 &quot;Origin of the car&quot;=&quot;Origin&quot;,
                 &quot;Prices&quot;=&quot;Price&quot;,
                 &quot;Rev /min&quot;=&quot;RPM&quot;,
                 &quot;Weight of car&quot;=&quot;Weight&quot;,
                 &quot;MPG city&quot;=&quot;MPG.city&quot;,
                 &quot;Horsepower of car&quot;=&quot;HorsePower&quot;,
                 &quot;Passengers capacity&quot;=&quot;Passengers&quot;,
                 &quot;Length of car&quot;=&quot;Length&quot;,
                 &quot;Manufacturer of car&quot;=&quot;Manufacturer&quot;,
                 &quot;Model of car&quot;=&quot;Model&quot; )
  
    output$main_plot &lt;- renderPlot({
      if(is.numeric(input$X_variable) &amp; (is.numeric(input$Y_variable))){
      ggplot(data2, aes(x = data2[,input$X_variable], 
                        y=data2[,input$Y_variable])) + 
          geom_point(aes(shape = input$Y_variable,
                         color=input$Y_variable,size=input$Y_variable))  
      else if ((is.factor(input$X_variable) &amp; 
                is.numeric(input$Y_variable)) | 
               (is.factor(input$Y_variable) &amp; 
                is.numeric(input$X_variable))){   
        ggplot(data2, aes(x = data2[,input$X_variable], 
                       y=data2[,input$Y_variable])) +
          geom_boxplot(aes(shape = input$Y_variable,
                           color=input$Y_variable,size=input$Y_variable))  
    } 
   else { ggplot(data2, aes(data2[,input$X_variable])) + 
        geom_bar(aes(fill=input$X_variable)) 
      }
      })
    
  
    
  
  
  })
  shinyApp(ui, server)
  </code></pre>
  <p>It is only working fine for <code>else</code> part but not for <code>if</code> and <code>else if</code> part. What I am doing wrong?</p>