【问题标题】:Problem in inserting UI after removing the ui删除ui后插入UI的问题
【发布时间】: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)

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    这很正常,因为您的 removeUI 删除了 div(id = "inputList")。所以在那之后insertUIs 找不到这个 div。

    改用:

    removeUI(selector = "#inputList *", multiple = TRUE)
    

    CSS 选择器#inputList * 选择#inputList 的所有后代,但不选择#inputList

    【讨论】:

    • 感谢您的快速解决方案。你能不能给我指出一个地方,在那里我可以通过适当的解释详细了解 CSS 选择器.....这将对我将来有所帮助!
    猜你喜欢
    • 1970-01-01
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 1970-01-01
    相关资源
    最近更新 更多