【问题标题】:How to remove UI?如何删除用户界面?
【发布时间】:2020-12-02 21:37:03
【问题描述】:

我无法使用移除按钮移除 UI。我尝试添加这个

removeUI(
      selector = paste0("div:has(> #row", "_", input$addLine, " )"),
      multiple = TRUE,
      immediate = TRUE,
      session
    )

但它不起作用。当我单击删除按钮时,没有任何反应。我想知道我是否没有在removeUI 函数中正确指定selector

有没有办法让这个工作,或者我应该尝试别的?

这是一个可重现的例子:

library(shiny)

newlist <- as.list(c("LV1", "LV2", "x1", "x2", "x3", "x4", "x5", "x6"))
symbol <- as.list(c("=~", "~"))

row_ui <- function(id) {
  ns <- NS(id)
  fluidRow(
    column(2,
           uiOutput(ns("ui_placeholder"))),
    column(2,
           uiOutput(ns("ui_placeholder3"))),
    column(2, 
           uiOutput(ns("ui_placeholder2")))
  )
} 

row_server <- function(input, output, session) {

  return_value <- reactive({paste(input$variable1, input$symbol1, paste(input$variable2, collapse = "+"))})
  ns <- session$ns
  output$ui_placeholder <- renderUI({
    selectizeInput(ns("variable1"), "LV:", choices = c(' ', newlist), options=list(create=TRUE), selected = NULL)
  })
  
  output$ui_placeholder2 <- renderUI({
    selectizeInput(ns("variable2"), "Ind:", choices = c(' ', newlist), options=list(create=TRUE), selected = NULL, multiple = TRUE)
  })
  
  output$ui_placeholder3 <- renderUI({
    selectizeInput(ns("symbol1"), "Type", choices = c(' ', symbol), options=list(create=TRUE), selected = NULL)
  })
  
  list(return_value = return_value) 
}

ui <- fluidPage(  
  div(id="placeholder"),
  actionButton("addLine", "+ LV"),
  actionButton("removeLine", "Remove UI")
)

server <- function(input, output, session) {
  
  handler <- reactiveVal(list())
  
  observeEvent(input$addLine, {
    new_id <- paste("row", input$addLine, sep = "_")
    insertUI(
      selector = "#placeholder",
      where = "beforeBegin",
      ui = row_ui(new_id)
    )
    
    handler_list <- isolate(handler())
    new_handler <- callModule(row_server, new_id)
    handler_list <- c(handler_list, new_handler)
    names(handler_list)[length(handler_list)] <- new_id
    handler(handler_list)
    
    removeUI(
      selector = paste0("div:has(> #row", "_", input$addLine, " )"),
      multiple = TRUE,
      immediate = TRUE,
      session
    )
})
}

shinyApp(ui, server)

【问题讨论】:

    标签: jquery r user-interface shiny


    【解决方案1】:

    也许你正在寻找这个。

    library(shiny)
    
    newlist <- as.list(c("LV1", "LV2", "x1", "x2", "x3", "x4", "x5", "x6"))
    symbol <- as.list(c("=~", "~"))
    
    row_ui <- function(id) {
      ns <- NS(id)
      fluidRow(
        column(2,
               uiOutput(ns("ui_placeholder"))),
        column(2,
               uiOutput(ns("ui_placeholder3"))),
        column(2, 
               uiOutput(ns("ui_placeholder2")))
      )
    } 
    
    row_server <- function(input, output, session) {
      
      return_value <- reactive({paste(input$variable1, input$symbol1, paste(input$variable2, collapse = "+"))})
      ns <- session$ns
      output$ui_placeholder <- renderUI({
        selectizeInput(ns("variable1"), "LV:", choices = c(' ', newlist), options=list(create=TRUE), selected = NULL)
      })
      
      output$ui_placeholder2 <- renderUI({
        selectizeInput(ns("variable2"), "Ind:", choices = c(' ', newlist), options=list(create=TRUE), selected = NULL, multiple = TRUE)
      })
      
      output$ui_placeholder3 <- renderUI({
        selectizeInput(ns("symbol1"), "Type", choices = c(' ', symbol), options=list(create=TRUE), selected = NULL)
      })
      
      list(return_value = return_value) 
    }
    
    
    ui <- fluidPage(  
      div(id="placeholder"),
      actionButton("addLine", "+ LV"),
      actionButton("removeLine", "Remove UI")
    )
    
    server <- function(input, output, session) {
      
      handler <- reactiveVal(list())
      ### keep track of elements/lines inserted and not yet removed
      inserted <- c()
      observeEvent(input$addLine, {
        new_id <- paste("row", input$addLine, sep = "_")
        insertUI(
          selector = "#placeholder",
          where = "beforeBegin",
          ui = tags$div(
            div(row_ui(new_id)),
            id = new_id
          )
        )
        # inserted <<- c(new_id, inserted)  ##  removes first one first
        inserted <<- c(inserted, new_id)    ##  removes last  one first
        
        handler_list <- isolate(handler())
        new_handler <- callModule(row_server, new_id)
        handler_list <- c(handler_list, new_handler)
        names(handler_list)[length(handler_list)] <- new_id
        handler(handler_list)
    
      })
      
      observeEvent(input$removeLine, {
        removeUI(
          ## pass in appropriate div id
          selector = paste0('#', inserted[length(inserted)])
        )
        inserted <<- inserted[-length(inserted)]
      })
      
    }
    
    shinyApp(ui, server)
    

    【讨论】:

    • 就是这样。 . .!
    猜你喜欢
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-31
    • 1970-01-01
    • 1970-01-01
    • 2021-06-30
    • 2021-04-01
    • 1970-01-01
    相关资源
    最近更新 更多