我是 R Shiny 的新手。我正在尝试创建一个带有一些动态生成的按钮的应用程序。每个按钮都有一个与之关联的observeEvent。我想添加一个重置按钮来删除所有observeEvents。我浏览了一些链接,发现 destroy() 函数可以用于相同的用途,但我无法弄清楚如何使用它。有什么建议么?谢谢。
library(shiny)
ui<-fluidPage(
actionButton(inputId = "add",label = "Add Stuff"),
htmlOutput("obs"),
actionButton("reset","Reset")
)
server<-function(input,output,session){
output$obs<-renderUI(HTML({names(input)}))
observeEvent(input$add,{
addModal<-function(failed=FALSE){
modalDialog(size = "l",easyClose = T,title = "Add",footer = "",
selectInput(inputId = "stuff",label = "Select",choices = c("A","B","C","D","E")),
numericInput(inputId = "numstuff",label = "Quantity",min = 1,max = 5,value = 1),
actionButton(inputId = "add2",label = "Add")
)
}
showModal(addModal())
})
num<<-0
observeEvent(input$add2,{
num<<-num+1
insertUI(selector = "#add",where = "beforeBegin",
ui = actionButton(inputId = paste0("comp",num),label = paste0(input$stuff,":",input$numstuff))
)
lapply(num:num,function(i){
observeEvent(input[[paste0("comp",i)]],{
modModal<-function(failed=FALSE){
modalDialog(size = "l",easyClose = T,title = "Delete",footer = "",
# numericInput(inputId = "newquan",label = "New Quantity",min=1,max=5,value=1),
actionButton(inputId = paste0("gomod",i),label = "Delete")
)
}
showModal(modModal())
},ignoreInit = T)
observeEvent(input[[paste0("gomod",i)]],{
nm<-paste0("#comp",i)
removeUI(selector=nm)
})
})
})
observeEvent(input$reset,{
observers<-names(input)
lapply(observers,function(x){
if(substr(x,1,4)=="comp"){
obs<-input[[x]]
obs$destroy()
}
})
})
}
shinyApp(ui=ui,server=server)