r - 在 R Shiny 中触发动态 SelectInput 的 ObserveEvent
问题描述
我在下面的代码中生成动态选择输入,为每一行创建单独的 UI。我想根据选择的选择显示或隐藏其他选择输入。我一直在尝试绑定ObserveEvent
selectinput,为此我搜索了很多但未能找到。
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。可能吗?
解决方案
推荐阅读
- python - 分词功能
- javascript - 如何在 React-three-fiber 中设置光线投射
- java - NoClassDefFoundError 使用“-jar”运行 Java 终端命令
- javascript - 如何在 JavaScript 中从 Django REST 获取数据
- string - READABLE_STRING_GENERAL 与 STRING
- reactjs - Stackexchange REST API 已被 CORS 策略阻止
- c - 仅在一个函数中未声明的变量
- javascript - 无法在 node.js Web 应用程序中显示 EJS 页面
- python-3.x - df.Commission.value - AttributeError:“系列”对象没有属性“值”
- python - 如何处理预测任务中的多个类别