R Shiny选择观察所有选择的观察事件

时间:2018-01-19 10:27:01

标签: r triggers shiny observable

我有以下Shiny应用程序,我希望只有在从selectizeInput UI做出所有选择时,observeEvent才会触发。这样做的原因是我有一个动态数量的maxItems(我在这个例子中任意设置为2)所以我不能只将observeEvent基于length。重要的是我还有一个在observeEvent上运行的脚本,我只想运行一次 - 一旦selectizeInput被“取消选择”。另一种方法是使用动作按钮,但我希​​望尽可能保持应用程序的自动化/平滑性。

下面的可重复示例

library(shiny)


ui <- fluidPage(

   uiOutput("server_lala")

   ,
   textOutput("choices")

)


server <- function(input, output)
{

   output$server_lala <- renderUI({


      temperature <- c(7.0, 6.9, 9.5, 14.5, 18.2, 21.5, 25.2, 26.5, 23.3, 18.3, 13.9, 9.6)

      selectizeInput( inputId = "lala",
                      choices = paste(temperature),
                      label= "2. Select type",
                      multiple = TRUE,
                      size = 10,
                      width = '100%',
                      options = list( placeholder = 'Type',
                                      maxItems = 2
                      )
      )
   })

   observeEvent(input$lala  , {

      output$choices <- renderText({
         paste("Selectize has a length of", input$lala)
      })

   })
}

shinyApp(ui = ui, server = server)

1 个答案:

答案 0 :(得分:0)

也许尝试这样的事情:

library(shiny)

ui <- fluidPage(

  numericInput('howmany','How many inputs?',min=1,max=10,value=2,step=1),
  selectizeInput('myselect','select options',multiple=TRUE, choices= seq(1,10), options = list(placeholder='type')),
  textOutput('mytext')

)


server <- function(input, output,session)
{

  all_selected <- reactiveVal('Not all inputs selected.')

  observeEvent(input$howmany, {
    updateSelectizeInput(session, 'myselect', options = list(maxItems = input$howmany))
  })

  observeEvent(input$myselect,{
    if(length(input$myselect)==input$howmany)
    {
      all_selected('All inputs selected!')
    }
  })

  output$mytext <- renderText({all_selected()})
}

shinyApp(ui = ui, server = server)