您可以使用闪亮的模块:您可以拥有相同的可变数字actionButton
。这些在ab_moduleUI
部分中定义。他们由他们自己的observeEvent
处理,但它只能在ab_module
部分中定义一次。
使用lapply
可以创建任意数量的actionButton
。
编辑:您不必事先指定按钮的数量:使用renderUI
在服务器端生成UI元素。
为了演示目的,我添加了一个numericInput
来增加/减少要渲染的模块数量。
# UI part of the module
ab_moduleUI <- function(id){
ns <- NS(id)
tagList(
fluidRow(
actionButton(ns("btn"), paste("ActionButton", id, sep="-")),
textOutput(ns("txt"))
)
)
}
# Server part of the module
ab_module <- function(input, output, session){
observeEvent(input$btn,{
output$txt <- renderText("More information shown")
})
}
# UI
ui <- fluidPage(
# lapply(paste0("mod", 1:no_btn), ab_moduleUI)
numericInput("num", "Number of buttons to show" ,value = 5, min = 3, max = 10),
uiOutput("ui")
)
# Server side
server <- function(input, output, session){
observeEvent(input$num, {
output$ui <- renderUI({
lapply(paste0("mod", 1:input$num), ab_moduleUI)
})
lapply(paste0("mod", 1:input$num), function(x) callModule(ab_module, x))
})
}
shinyApp(ui, server)
了解更多关于光泽模块here
来源
2016-09-27 08:08:26
GyD
还有一个闪亮的submitButton也是http://shiny.rstudio。com/gallery/submitbutton-demo.html –
@PorkChop - 感谢您的链接。我已经忘记了这一个。但不幸的是,'submitButton'捕获整个页面上的所有事件,直到您点击它。在标准HTML中,它只会是该按钮的'
您可以将所有按钮绑定到一个事件,使用'eventReactive'或'observeEvent'注意,这些事件可以包含多个由逗号分隔的事件。 http://shiny.rstudio.com/reference/shiny/latest/observeEvent.html –