【发布时间】:2019-01-22 11:50:57
【问题描述】:
Here我问了一个类似的问题并得到了一个有效的答案。但是,如果子段的“actionButton”被“selectInput”替换,则该解决方案不起作用。在每次选择 selectInput 时都会创建两个输出。请帮忙..谢谢....
library(shiny)
ui <- fluidPage(
verbatimTextOutput("txt",placeholder = T), #"It is Created for Testing"
actionButton("addSeg", "Add a Segment"),
uiOutput("myUI")
)
server <- function(input, output, session) {
alld <- reactiveValues()
alld$ui <- list()
# Action to add new Segment
observeEvent(input$addSeg,{
new_id <- length(alld$ui) + 1
sub_name <- paste0("addSub_", new_id)
alld$ui[[new_id]] <- list(selectInput(sub_name,"Add a variable", choices = c("V1","V2"), selected = NULL))
observeEvent(input[[sub_name]], {
new_text_id <- length(alld$ui[[new_id]]) + 1
alld$ui[[new_id]][[new_text_id]] <- HTML(paste0("Variable ",input[[sub_name]]," added<br>"))
}, ignoreInit = TRUE)
})
output$myUI <- renderUI({alld$ui})
output$txt <- renderText({class(alld$ui)})
}
shinyApp(ui, server)
【问题讨论】:
-
顺便说一句,你为什么要用 selectInput 替换 actionButton?
-
@amrrs 这是一个示例代码。在我原来的闪亮应用程序中需要替换,它更复杂..