我目前正在研发一个基于R Shiny的Web应用程序,它基于动态创建的UI上的功能,包括渲染其他输入字段,即selectInput
。 selectInputs
的inputId
属性也是动态创建的,我无法预先定义创建的selectInputs
的数量,因为它也会根据用户的输入而变化。注册事件处理程序动态添加selectInput
当我想对用户更改动态创建的selectInput
中的选定值作出反应时,会出现此问题。然而
observeEvent(input$select_inputId, { ... })
,我不能抢先注册数目不详尚未指定inputIds
的事件:通常情况下,如果selectInput
是“静”,我会observeEvent注册。有没有可能将某种事件处理程序传递给selectInput
或其他解决方案?
下面是代码动态创建我的用户界面的重要组成部分:
observeEvent(input$rate_criterion_select, {
output$rate_criterions_ratios <- renderUI({
container <- tags$div()
for(i in seq(1, length(children_criterions))) {
container <- tagAppendChild(container, selectInput(
inputId = paste(input$rate_criterion_select, i)
label = ""
choices = rates
)
}
return(container)
})
})
编辑:下面你可以找到闪亮的应用程序的可重复的例子。解决方案是将用户提供的值存储在变量choices
中。例如:如果用户选择3个输入来填充并相应地选择“一个”,“两个”和“三个”,则choices
应该是矢量c("One","Two","Three")
;如果用户选择了2个输入来填充并选择“Two”,“Two” - choices
应该是c("Two","Two")
。
server.R
library(shiny)
# choices <-
shinyServer(function(input, output) {
observeEvent(input$how_many, {
output$render_area <- renderUI({
container <- tags$div()
for(i in seq(1, as.numeric(input$how_many))) {
container <- tagAppendChild(container, selectInput(
inputId = paste("selection", i),
label = paste("Selection", i),
choices = c("One", "Two", "Three"),
selected = "One"
))
}
return(container)
})
})
})
ui.R
library(shiny)
shinyUI(fluidPage(
titlePanel("Example App"),
sidebarLayout(
sidebarPanel(
selectInput("how_many", "How many inputs do you want to fill?", c("1", "2", "3", "4", "5"))
),
mainPanel(
uiOutput("render_area")
)
)
))
请发布一个可重复的示例 - 其他人可以运行的完整应用的代码 –
我刚刚添加了完整示例应用的代码,希望能够清楚地说明问题所在。 –