mercredi 15 juillet 2020

checkboxInput in R shiny

I have a question about the checkboxInput in R shiny. When it is checked, the scatter plot should be colorful while when it is unchecked, the plot should be black. I have tried several methods, but it keeps colorful no matter whether it is checked or not. Could you please help me with fix the code? Thanks so much.

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


# Start a 'Shiny' part
shinyServer(function(input, output, session) {
  
  # Create a new reactive variable
  newVar <- reactive({
    newData <- msleep %>% filter(vore == input$vore)
  })
  
  
  # Create a scatter plot
  output$sleepPlot <- renderPlot({
    newDat <- newVar()
    g <- ggplot(newDat, aes(x = bodywt, y = sleep_total))
    g + geom_point(size = input$size, aes(col = conservation))
  })

  # Create text info
  output$info <- renderText({
    newDat <- newVar()
    paste("The average body weight for order", input$vore, "is", round(mean(newDat$bodywt, na.rm = TRUE), 2), 
          "and the average total sleep time is", round(mean(newDat$sleep_total, na.rm = TRUE), 2), sep = " ")
  })
  
  # Create output of observations
  output$table <- renderTable({
    newDat <- newVar()
        newDat
  })
})

library(ggplot2)

shinyUI(fluidPage(
  
  # Application title
  titlePanel("Investigation of Mammal Sleep Data"),
  
  # Sidebar with options for the data set
  sidebarLayout(
    sidebarPanel(
      h3("Select the mammal's biological order:"),
      selectizeInput("vore", "Vore", selected = "omni", 
                     choices = levels(as.factor(msleep$vore))),
      br(),
      sliderInput("size", "Size of Points on Graph",
                  min = 1, max = 10, value = 5, step = 1),
      checkboxInput("conservation", h4("Color Code Conservation Status", style = "color:red;"))
    ),
    
    # Show output
    mainPanel(
      plotOutput("sleepPlot"),
      textOutput("info"),
      tableOutput("table")
      )
  )
))




Aucun commentaire:

Enregistrer un commentaire