繁体   English   中英

复选框输入 R shiny

[英]checkboxInput in R shiny

我对 R shiny 中的 checkboxInput 有疑问。 选中时,散点图 plot 应该是彩色的,而未选中时,plot 应该是黑色的。 我尝试了几种方法,但无论是否检查它都保持彩色。 你能帮我修复代码吗? 非常感谢。

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")
      )
  )
))

尝试这个

 # Create a scatter plot
  output$sleepPlot <- renderPlot({
    newDat <- newVar()
    colorme <- unique(newVar()$conservation)
    ncolor <- length(colorme)
    if (!input$conservation) {
      mycolor <- c(rep("black",ncolor))
      mylabels <- c(rep(" ",ncolor))
    }

    g <- ggplot(newDat, aes(x = bodywt, y = sleep_total)) +
      geom_point(size = input$size, aes(col = conservation)) +
      {if (!input$conservation) scale_color_manual(name=" ", values=mycolor, labels=mylabels)} +
      { if (!input$conservation) guides(color='none')}
    g
  })

您可以根据需要进行调整。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM