【发布时间】:2020-06-14 14:15:55
【问题描述】:
我正在尝试删除闪亮应用中的 ui。我有使用单独的删除按钮分别删除每个 ui 元素的代码。但另外我还想要一个通用的删除按钮来一次删除所有插入的用户界面。对于任务,我尝试了 insertUI 中的选择器,并且能够删除所有添加的 UI。
但是在完成这个特定任务之后,即在一次删除所有 UI 之后,Shiny 不允许我插入任何新的 UI。似乎部门 ID 已被永久删除,即使在调用添加按钮后也无法重新生成。如何解决这个问题,请任何人指导我。
下面是可行的代码,取自stackoverflow
library(shiny)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
actionButton('addBtn', 'Add input Set'),
actionButton("remove","Delete"),
tags$div(id='inputList')
),
mainPanel()
))
server <- function(input, output) {
observeEvent(input$addBtn, {
# Increment by 1
nr <- input$addBtn + 1
# Creating a list of IDs based on our increments
id <- paste0("input",nr)
row_id <- paste0("newInput",nr)
# Inserting the UIs
insertUI(
selector = '#inputList',
ui=div(
id = row_id,
selectizeInput(
inputId = id,
choices = c("Stuff","to","input"),
selected = c("Stuff"),
label = "An Input:"
),
actionButton(paste0('removeBtn',nr), 'Remove')
)
)
# Removes UI seperately
# Uses Seperate remove button for each ui
observeEvent(input[[paste0('removeBtn',nr)]],{
shiny::removeUI(
selector = paste0("#newInput",nr)
)
})
})
# Remove all the added UIs at once
# InsertUI doesn't work after this
observeEvent(input$remove,{
removeUI(selector = "#inputList")
})
}
shinyApp(ui, server)
【问题讨论】: