Plot the boxplot by using check box in shiny app

Viewed 504

I'd like to use check box input to allow to show different island levels (within the categorical variable selected for the x-axis) with separate boxplots and different colors with a legend. But if this check box is not selected, I just want to show boxplot without fill=legend that is:

ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis)) +
            geom_boxplot()

This R code is what I tried to use but It didn't work. Could you please help me to solve or tell me what makes error with my R code? Thank you in advance

library(shiny)
library(palmerpenguins)
library(ggplot2)
library(dplyr)

penguin <- penguins

penguin$year <- as.factor(penguin$year)

ui <- fluidPage(
  titlePanel("Data Visualisation of Penguins Data"),
  sidebarPanel(
    selectInput("yaxis",
                label = "Choose a y-axis variable to display",
                choices = list("bill_length_mm",
                               "bill_depth_mm",
                               "flipper_length_mm",
                               "body_mass_g"),
                selected = "bill_length_mm"),
    selectInput("xaxis",
                label = "Choose a x-axis variable to display",
                choices = c("species",
                            "sex",
                            "year"),
                selected = "sex"),
    checkboxGroupInput("islandlevels",
                       label = "Check to display different island levels",
                       choices = c("island"),
                       selected = NULL),
    br(), br(),
    selectInput("species",
                label = "Choose species to view separate plot",
                choices = list("Adelie",
                               "Chinstrap",
                               "Gentoo"),
                selected = NULL)),
  mainPanel(
    plotOutput("plot1"),
    br(), br(),
    plotOutput("plot2")
  )
)

server <- function(input, output){
  dat <- reactive({
    if(input$xaxis == "sex") penguin[!is.na(penguin$sex),] else penguin
  })
  output$plot1 <- renderPlot({
    if(input$islandlevels == "island") {
      req(penguin, input$xaxis, input$yaxis)
      ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis, fill=island)) +
        geom_boxplot()
    }
    if(input$islandlevels = NULL) {
      req(penguin, input$xaxis, input$yaxis) 
          ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis)) +
            geom_boxplot()}
})
}

shinyApp(ui = ui, server = server)
1 Answers
  1. As long as you don't want any other checkbox inputs you could use a checkboxInput instead of a checkboxGroupInput which makes checking a bit easier.

  2. One issue in your server was that you used island instead of "island". Additionally you can simplify your code a little bit by using fill <- if (input$islandlevels) "island" which will return NULL is the the checkbox was not checked and "island" otherwise. This way you can handle both case with only one ggplot statement .

The full reproducible code:

library(shiny)
library(palmerpenguins)
library(ggplot2)
library(dplyr)

penguin <- penguins

penguin$year <- as.factor(penguin$year)

ui <- fluidPage(
  titlePanel("Data Visualisation of Penguins Data"),
  sidebarPanel(
    selectInput("yaxis",
                label = "Choose a y-axis variable to display",
                choices = list("bill_length_mm",
                               "bill_depth_mm",
                               "flipper_length_mm",
                               "body_mass_g"),
                selected = "bill_length_mm"),
    selectInput("xaxis",
                label = "Choose a x-axis variable to display",
                choices = c("species",
                            "sex",
                            "year"),
                selected = "sex"),
    checkboxInput("islandlevels",
                       label = "Check to display different island levels",
                       value = FALSE),
    br(), br(),
    selectInput("species",
                label = "Choose species to view separate plot",
                choices = list("Adelie",
                               "Chinstrap",
                               "Gentoo"),
                selected = NULL)),
  mainPanel(
    plotOutput("plot1"),
    br(), br(),
    plotOutput("plot2")
  )
)

server <- function(input, output){
  dat <- reactive({
    if(input$xaxis == "sex") penguin[!is.na(penguin$sex),] else penguin
  })
  output$plot1 <- renderPlot({
    req(penguin, input$xaxis, input$yaxis) 
    fill <- if (input$islandlevels) "island"
    ggplot(dat(), aes_string(x = isolate(input$xaxis), y = input$yaxis, fill = fill)) +
        geom_boxplot()
  })
}

shinyApp(ui = ui, server = server)
Related