触发ObserveEvent以在R Shiny中进行动态SelectInput

时间:2019-01-18 11:07:37

标签: r shiny

我正在以下代码中生成动态selectinput,为每一行创建单独的UI。我想基于选择的选择显示或隐藏其他selectinput。我一直在尝试将ObserveEvent绑定到selectinput上,为此我在Google上搜索了很多,但是没有找到。

    library(shiny)   
    library(shinyWidgets)

    ui = fluidPage(  
         absolutePanel(
          left="20px",
          top="250px",
          width="auto",
          id="listPanel",

          column(
            width=6,

            uiOutput("redeploymentViewUI")

            )
          ) 
    )
    server = function(input,output,session){
         output$filtersUI=renderUI({
            div(
              div(


              ID="firstRow",
              div(
                style="display: inline-block;vertical-align:top;width: 35px;padding-top:5%;cursor:pointer;",
                actionButton(
                  "AddButton_1",
                  HTML("<i class='fas fa-plus' style='color:black;' title='Add more filter'></i>"),
                  class="filterbuttons",
                  onclick="Shiny.onInputChange('Add_Row_button','1')"
                )

              ),
              div(
                style="display: inline-block;vertical-align:top;width: 150px;",
                # Filter by Segment - I want to trigger here ObserverEvent
                selectInput(
                  inputId="FilterField_1",
                  label="Field",
                  choices=c("Employee","State","Status","Supervisor"),
                  multiple=F,
                  selectize = F
                )
              ),
              div(
                style="display: inline-block;vertical-align:top;width: 150px;",
                selectInput(
                  inputId="FilterOperator_1",
                  label="Operator",
                  choices=c("Begins with","Equals to","Not Equals to","Contains","Not Contains"),
                  multiple=F,
                  selectize = F

                )
              ),
              div(
                style="display: inline-block;vertical-align:top;width: 150px;display:none",
                selectInput(
                  inputId="FilterState_1",
                  label="State",
                  choices=sort(unique(c("",df$db$WORK_STATE))),
                  multiple=F,
                  selectize = F
                )
              ),
              div(
                style="display: inline-block;vertical-align:top;width: 200px;",
                textInput("FilterValue_1", "Value")
              )
              ),
              br(),
              br(),
              div(
                style="display: inline-block;vertical-align:top;width: 150px;",
                actionButton("applyFilter", "Apply Filter"),
                actionButton("reset", "Clear")
              )

            )

          })

          # Redeployment View UI -----------------------------
          output$redeploymentViewUI = renderUI({

            box(
              width=NULL,
              solidHeader=TRUE,
              title="Redeployment View",

              uiOutput("buttonsUI"),
              br(),
              uiOutput("filtersUI"),
              br(),
              dataTableOutput("redeployView")

            )

          })
observeEvent(input$Add_Row_button,{
    if(input$Add_Row_button!=""){

    filterRow$CurrentRow= filterRow$CurrentRow +1


    insertUI(
      selector = "#firstRow",
      where = "beforeEnd",
      ui=({
        div(
          id=paste("div",filterRow$CurrentRow,sep = ""),
          div(
            style="display: inline-block;vertical-align:top;width: 35px;padding-top:4%;cursor:pointer;",
            actionButton(
              inputId=paste("DeleteButton",filterRow$CurrentRow,sep = "_"),
              class="filterbuttons",
              label = HTML("<i class='fas fa-minus' style='color:black;' title='Remove this'></i>"),
              onclick=paste0("Shiny.onInputChange('Delete_Row_button',",filterRow$CurrentRow,")")

            )

          ),
          div(
            style="display: inline-block;vertical-align:top;width: 150px;",
           # Filter by Segment - I want to trigger here ObserverEvent same event of above code. 
            selectInput(
              inputId=paste("FilterField",filterRow$CurrentRow,sep = "_"),
              label="",
              choices=c("Employee","State","Status","Supervisor"),
              multiple=F,
              selectize = F
            )
          ),

         div(
          style="display: inline-block;vertical-align:top;width: 150px;",
          selectInput(
            inputId=paste("FilterOperator",filterRow$CurrentRow,sep = "_"),
            label = "",
            choices=sort(unique(c("Begins with","Equals to","Not Equals to","Contains","Not Contains"))),
            multiple=F,
            selectize = F

          )
        ),
        div(
          style="display: inline-block;vertical-align:top;width: 150px;display:none",
          selectInput(
            inputId=paste("FilterState",filterRow$CurrentRow,sep = "_"),
            label="",
            choices=sort(unique(c("",df$db$WORK_STATE))),
            multiple=F,
            selectize = F

          )
        ),
        div(
          style="display: inline-block;vertical-align:top;width: 200px;",
          textInput(paste("FilterValue",filterRow$CurrentRow,sep = "_"),label =" ")
        ),
        br()

        )
      })
    )
    }
    runjs("Shiny.onInputChange('Add_Row_button','');")
  })

  observeEvent(input$Delete_Row_button,{
    if(input$Delete_Row_button!=""){
      filterCRow<- as.numeric(input$Delete_Row_button)
      removeUI(selector = paste("div#div",filterCRow,sep=""))
      filterRow$CurrentRow= filterRow$CurrentRow-1
    }

    runjs("Shiny.onInputChange('Delete_Row_button','');")
  })
    }

我们可以为所有生成的SelectInput绑定相同的ObserveEvent吗?有可能吗?

0 个答案:

没有答案