rshinyhistogramiris-dataset

How to create shinyapp for multiple histogram plots?


I am working with the iris dataset to create histograms for the four numerical variables with individual sliders for specifying a maximum value for the variable, i.e., the slider for “Sepal.Width” could be adjusted from 2.2 to 4.4 and if the user changes the slider to 3, then the histogram will only show those data points from 2.2 to 3. I tried giving it a go, but can't figure out how to proceed.

library(tidyverse)
library(shiny)
data(iris)
ui <- fluidPage(
    titlePanel("Shiny app for IRIS data"),
    sidebarLayout(
        sidebarPanel(
            # For selecting a required species 
            selectInput( "species", "Select Species to plot:",
                         choices = c("setosa" = 1,
                                     "versicolor" = 2, 
                                     "virginica" = 3)),
            # For selecting the max value for slider input
            sliderInput("lengths1","Sepal Length", min=0.1,max=8.0,value = 4.0,step = 0.1),
            
            sliderInput("lengths2","Sepal Width", min=0.1,max=8.0,value = 4.0,step = 0.1),
            
            sliderInput("lengths3","Petal Length",min=0.1,max=8.0,value = 4.0,step = 0.1),
            
            sliderInput("lengths4","Petal Width", min=0.1,max=8.0,value = 4.0,step = 0.1),
            
            textInput("title", "Title", value = "Enter desired Title")
        ),
        mainPanel( plotOutput(outputId = "plot1")
        )
    )
)

server <- function(input, output){}

shinyApp(ui, server)

Solution

  • Here is a first pass. There are some small bugs and I can add an improved version if you want but I think it answers your question and should set you on the right track. I am not sure what your "Enter desired Title" is about so I have left that part. Basically, you need to add the server section of the shiny app which is where the actual filtering of the data and plotting occurs.

    library(tidyverse)
    library(shiny)
    data(iris)
    ui <- fluidPage(
        titlePanel("Shiny app for IRIS data"),
        sidebarLayout(
            sidebarPanel(
                # For selecting a required species 
                selectInput( "species", "Select Species to plot:",
                             choices = sort(unique(iris$Species))),
                # For selecting the max value for slider input
                sliderInput("sl_max","Sepal Length", min=min(iris$Sepal.Length),max=max(iris$Sepal.Length),value = median(iris$Sepal.Length),step = 0.1),
                
                sliderInput("sw_max","Sepal Width", min=min(iris$Sepal.Width),max=max(iris$Sepal.Width),value = median(iris$Sepal.Width),step = 0.1),
                
                sliderInput("pl_max","Petal Length", min=min(iris$Petal.Length),max=max(iris$Petal.Length),value = median(iris$Petal.Length),step = 0.1),
                
                sliderInput("pw_max","Petal Width", min=min(iris$Petal.Width),max=max(iris$Petal.Width),value = median(iris$Petal.Width),step = 0.1),
                
                textInput("title", "Title", value = "Enter desired Title")
            ),
            mainPanel( plotOutput(outputId = "sl"),
                       plotOutput(outputId = "sw"),
                       plotOutput(outputId = "pl"),
                       plotOutput(outputId = "pw")
            )
        )
    )
    
    server <- function(input, output){
        data <- reactive({
            iris %>% filter(Species == input$species)
        })
        
        output$sl <- renderPlot({
            data <- data() %>% filter(Sepal.Length <= input$sl_max)
            hist(data$Sepal.Length)
        })
        
        output$sw <- renderPlot({
            data <- data() %>% filter(Sepal.Width <= input$sw_max)
            hist(data$Sepal.Width)
        })
        
        output$pl <- renderPlot({
            data <- data() %>% filter(Petal.Length <= input$pl_max)
            hist(data$Petal.Length)
        })
        
        output$pw <- renderPlot({
            data <- data() %>% filter(Petal.Width <= input$pw_max)
            hist(data$Petal.Width)
        })
    }
    
    shinyApp(ui, server)