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)
答案 0 :(得分:1)
之所以会发生此行为,是因为每次将新元素添加到列表时,都会重新渲染自定义UI元素。单击“ V2”并添加新的文本元素后,selectInput
本身将重新呈现并重置为V1,这已由您创建的观察者注意到。
以下可能是您的解决方案:
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 = "")
)
observeEvent(input[[sub_name]], {
if (input[[sub_name]] == "") return()
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)
})
我在这里所做的是向您的selectInput
添加一个空选项,并为相应的观察者添加了一个条件,即如果输入为空,它不应该执行任何操作。这样,我利用了“重置”行为而不是烦人。