Search code examples
rshinyshinydashboardshinyappsshiny-reactivity

R shiny variable graphs


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

  1. If 2 numerical variables are selected plot a scatter plot
  2. if one categorical and one numerical variable is selected plot a boxplot
  3. if two categorical variables are selected plot a barplot

The code I have tried till now

library(shiny)
library(ggplot2)
library(readr)
library(tidyverse)


df  <- read_table("cars.xls")

ui <- shinyUI(fluidPage(
  
  # Application title
  titlePanel("Car Dataset Analysis"),
  
  # Sidebar with a slider input for number of bins
  sidebarLayout(
    sidebarPanel(
      selectInput("X_variable", "X Variable:", 
                  c("No of Cylinders" = "Cylinders",
                    "Type of car" = "Type",
                    "Engine Size" = "EngineSize",
                    "Fuel Tank capacity" = "Fuel.tank.capacity",
                    "Origin of the car"="Origin",
                    "Prices"="Price",
                    "Rev /min"="RPM",
                    "Weight of car"="Weight",
                    "MPG city"="MPG.city",
                    "Horsepower of car"="HorsePower",
                    "Passengers capacity"="Passengers",
                    "Length of car"="Length",
                    "Manufacturer of car"="Manufacturer",
                    "Model of car"="Model" )),
      selectInput("Y_variable", " Y Variable:", 
                  c("No of Cylinders" = "Cylinders",
                    "Type of car" = "Type",
                    "Engine Size" = "EngineSize",
                    "Fuel Tank capacity" = "Fuel.tank.capacity",
                    "Origin of the car"="Origin",
                    "Prices"="Price",
                    "Rev /min"="RPM",
                    "Weight of car"="Weight",
                    "MPG city"="MPG.city",
                    "Horsepower of car"="HorsePower",
                    "Passengers capacity"="Passengers",
                    "Length of car"="Length",
                    "Manufacturer of car"="Manufacturer",
                    "Model of car"="Model" )),

    mainPanel(
      tabsetPanel(type="tabs",
                  
      tabPanel("Plots",
        plotOutput("main_plot")),  

      )
    )
  )
))

server <- shinyServer(function(input, output) {
  
  data <- df
  
  
  data$Model <- factor(data$Model)
  data$Manufacturer <- factor(data$Manufacturer)
  data$Origin <- factor(data$Origin)
data2 <- data.frame(data)
  
  choices <- c("No of Cylinders" = "Cylinders",
               "Type of car" = "Type",
               "Engine Size" = "EngineSize",
               "Fuel Tank capacity" = "Fuel.tank.capacity",
               "Origin of the car"="Origin",
               "Prices"="Price",
               "Rev /min"="RPM",
               "Weight of car"="Weight",
               "MPG city"="MPG.city",
               "Horsepower of car"="HorsePower",
               "Passengers capacity"="Passengers",
               "Length of car"="Length",
               "Manufacturer of car"="Manufacturer",
               "Model of car"="Model" )

  output$main_plot <- renderPlot({
    if(is.numeric(input$X_variable) & (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) & 
              is.numeric(input$Y_variable)) | 
             (is.factor(input$Y_variable) & 
              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)

It is only working fine for else part but not for if and else if part. What I am doing wrong?


Solution

  • Try this

    library(shiny)
    library(ggplot2)
    library(readr)
    library(tidyverse)
    library(readxl)
    
    df  <- read_xlsx("cars.xlsx")
    
    choices <- c("No of Cylinders" = "Cylinders",
                 "Type of car" = "Type",
                 "Engine Size" = "EngineSize",
                 "Fuel Tank capacity" = "Fuel.tank.capacity",
                 "Origin of the car"="Origin",
                 "Prices"="Price",
                 "Rev /min"="RPM",
                 "Weight of car"="Weight",
                 "MPG city"="MPG.city",
                 "Horsepower of car"="HorsePower",
                 "Passengers capacity"="Passengers",
                 "Length of car"="Length",
                 "Manufacturer of car"="Manufacturer",
                 "Model of car"="Model" )
    
    ui <- shinyUI(fluidPage(
      
      # Application title
      titlePanel("Car Dataset Analysis"),
      
      # Sidebar with a slider input for number of bins
      sidebarLayout(
        sidebarPanel(
          selectInput("X_variable", "X Variable:", choices = choices),
          selectInput("Y_variable", " Y Variable:", choices = choices)
        ),
          mainPanel(
            tabsetPanel(type="tabs",
                        tabPanel("Plots", plotOutput("main_plot")),  
                        
            )
          )
        )
    ))
      
    server <- shinyServer(function(input, output) {
        
        data <- df
        
        data$Model <- factor(data$Model)
        data$Manufacturer <- factor(data$Manufacturer)
        data$Origin <- factor(data$Origin)
        data2 <- data.frame(data)
        
        output$main_plot <- renderPlot({
          x <- data2[[input$X_variable]]
          y <- data2[[input$Y_variable]]
          if(is.numeric(x) & (is.numeric(y))){
            ggplot(data2, aes(x = .data[[input$X_variable]], y=.data[[input$Y_variable]])) + 
              geom_point(aes(shape = input$Y_variable, color=input$Y_variable, size=input$Y_variable))  
            }else if ((is.factor(x) & is.numeric(y)) | (is.factor(y) & is.numeric(x))) {
              ggplot(data2, aes(x = .data[[input$X_variable]], y=.data[[input$Y_variable]])) +
                geom_boxplot(aes(shape = input$Y_variable, color=input$Y_variable, size=input$Y_variable))  
            } 
            else {
              ggplot(data2, aes(.data[[input$X_variable]])) + geom_bar(aes(fill=input$X_variable)) 
            }
          })
        
    })
    shinyApp(ui, server)