Shiny:有条件更新SelectizeInput选项的问题

时间:2017-06-27 11:01:19

标签: r shiny shinydashboard

我想选择一个对输出有影响的选项。这里有两个选项:Car,Cylinders可供选择。根据所选选项,他们有不同的选择。

library(shiny)

mtcars$cars <- rownames(mtcars)

ui <- fluidPage(
  title = 'Select experiment',
  sidebarLayout(
    sidebarPanel(
      radioButtons("selectOpt", "Want to select:",
                   list("Car"='car', "Cylinders"='cyl'),
                   selected = 'car'),

      conditionalPanel(
        condition = "input.selectOpt=='car'",
        selectizeInput(
          inputId = "sel_car", 
          label = "Select a Car",
          choices = mtcars$cars
        )),
      conditionalPanel(
        condition = "input.selectOpt=='cyl'",
        selectizeInput(
          inputId = "sel_cyl", 
          label = "Select Cylinders",
          choices = unique(mtcars$cyl)
        )
        )
    ),
    mainPanel(
      helpText('Output of the examples in the left:'),
      verbatimTextOutput('ex_out')
    )
  )
)

server <- function(input, output) {

    output$ex_out <- renderPrint({
      if(!is.null(input$sel_car)){
        cat(input$sel_car,input$sel_cyl,'\n')
        mtcars[mtcars$cars == input$sel_car,]
      }else if(!is.null(input$sel_cyl)){
        mtcars[mtcars$cyl == input$sel_cyl,]
      }
    })
}

shinyApp(ui, server)

然而,我观察到这两个选项总是选择了一个选项。这可以在输出的第一行(源cat(input$sel_car,input$sel_cyl,'\n'))中观察到。

选择 Car 时,也会选择Cylinders选项 6 中的第一个选项。 When **Car** is selected

当选择 Cylinders 选项时,Car选项 Mazda RX4 中的第一个选项也被选中。 When **Cylinders** option is selected

如何避免第一选择的默认选择,以便我能够评估ifelse if部分?

1 个答案:

答案 0 :(得分:1)

单选select(ize)Input()始终有一个选定的选项。 来自?selectInput

  

<强>选择
  最初选择的值(如果multiple = TRUE,则为多个值)。如果未指定,则默认为单选列表的第一个值,而不包含多个选择列表的值。

要在我们插入某种无效的&#39;之后获得您的功能。选择:

selectizeInput(
  inputId = "sel_car", 
  label = "Select a Car",
  choices = c('No car' = 0, mtcars$cars)
)

并在if.. else中检查:

if(input$sel_car != 0){
  cat(input$sel_car,input$sel_cyl,'\n')
  mtcars[mtcars$cars == input$sel_car,]
}

完整代码:

library(shiny)

mtcars$cars <- rownames(mtcars)

ui <- fluidPage(
  title = 'Select experiment',
  sidebarLayout(
    sidebarPanel(
      radioButtons("selectOpt", "Want to select:",
                   list("Car"='car', "Cylinders"='cyl'),
                   selected = 'car'),

      conditionalPanel(
        condition = "input.selectOpt=='car'",
        selectizeInput(
          inputId = "sel_car", 
          label = "Select a Car",
          choices = c('No car' = 0, mtcars$cars)
        )),
      conditionalPanel(
        condition = "input.selectOpt=='cyl'",
        selectizeInput(
          inputId = "sel_cyl", 
          label = "Select Cylinders",
          choices = c('No cylinder selected' = 0, unique(mtcars$cyl)),
          selected = NA
        )
      )
    ),
    mainPanel(
      helpText('Output of the examples in the left:'),
      verbatimTextOutput('ex_out')
    )
  )
)

server <- function(input, output) {

  output$ex_out <- renderPrint({
    cat(input$sel_car)
    if(input$sel_car != 0){
      cat(input$sel_car,input$sel_cyl,'\n')
      mtcars[mtcars$cars == input$sel_car,]
    }else if(input$sel_cyl != 0){
      mtcars[mtcars$cyl == input$sel_cyl,]
    }
  })
}

shinyApp(ui, server)