简体   繁体   中英

Multiple Condition in Shiny Dashboard

Below is an example and but in present situation i have lot of condition to be passed and i dont want to use the if statement. Is there any other way without using if i can get the work done.

Code


library(shiny)

ui = fluidPage(
  selectInput('p_id','ID:', c(111,222,333)),
  uiOutput('uiID')
)

server = function(input, output, session) {

  maxdays <- reactive({
    if(input$p_id %in% c(111)){
      x = 1
    }else{
      if(input$p_id %in% c(222)){
        x = 2
      }else
        x = 3 
    }
    return(x)
  })


  output$uiID <- renderUI({
    selectInput('days','Days:', choices=seq(1,maxdays()))
  })


}

runApp(shinyApp(ui = ui, server = server))

Assuming that maxdays basically returns the position of your selectInput 's choices, you could do the following:

library(shiny)

my_choices <- c(111, 222, 333)

ui <- fluidPage(
  selectInput('p_id', 'ID:', my_choices),
  uiOutput('uiID')
)

server <- function(input, output, session) {
  maxdays <- reactive(which(my_choices %in% input$p_id))

  output$uiID <- renderUI({
    selectInput('days', 'Days:', choices = seq(1,maxdays()))
  })
}

shinyApp(ui, server)

Its better not to re-create objects all the time using renderUI , instead we can simply update the widget:

library(shiny)

data <- c(111,222,333)
ui <- fluidPage(
  selectInput('p_id','ID:', data),
  selectInput('days','Days:', choices = NULL)
)

server = function(input, output, session) {

  observeEvent(input$p_id,{
    mseq <- seq(1,which(data %in% input$p_id))
    updateSelectInput(session,"days","Days:",choices = mseq)
  })

}
runApp(shinyApp(ui = ui, server = server))

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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