闪亮滑块取决于单个复选框

时间:2018-06-06 18:18:07

标签: r shiny shinydashboard

我有一个闪亮的应用程序,我试图只有选中了复选框时才会出现两个滑块。下面是我试图开始工作的代码,但没有看到UI。

library(shiny)

ui <- fluidPage(

  checkboxInput("box_checked", "box_checked", value = FALSE),
  uiOutput("test")
)

# Define server logic 
server <- function(input, output) {

  output$test = renderUI({
    if (input$box_checked = 0){
      return(NULL)
    }


    if(input$box_checked = 1){
      sliderInput("sliderOne", "Choose your value", min=0, max=100, value=50)
      sliderInput("sliderTwo", "Choose your other value", min=0, max=50, value=25)
    }

  })


}
# Run the application 
shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:1)

尝试这种方式:

library(shiny)

ui <- fluidPage(checkboxInput("box_checked", "box_checked", value = FALSE),
                uiOutput("test"))

# Define server logic
server <- function(input, output) {
  output$test = renderUI({
    if (input$box_checked == 0) {
      return(NULL)
    }


    if (input$box_checked == 1) {
      list(
        sliderInput(
          "sliderOne",
          "Choose your value",
          min = 0,
          max = 100,
          value = 50
        ),
        sliderInput(
          "sliderTwo",
          "Choose your other value",
          min = 0,
          max = 50,
          value = 25
        )
      )
    }

  })


}
# Run the application
shinyApp(ui = ui, server = server)
  1. 我修复了if语句,因为您使用的是input$box_checked = 1而不是input$box_checked == 1
  2. 您应该使用list()renderUI内生成多个UI元素。