【问题标题】:How to clean "input" after removing element using removeUI如何在使用 removeUI 删除元素后清理“输入”
【发布时间】:2018-12-02 18:37:12
【问题描述】:

如何在删除某些元素后清理“输入”(下面示例中的 verbatimTextOutput("summary"))。 我使用shiny.unbindAll 尝试了一些事情,但没有成功。 专用的removeUI 不做这项工作。 请看一下这个例子:

library(shiny)

ui <- fluidPage(

    actionButton('insertBtn', 'Insert'), 
    actionButton('removeBtn', 'Remove'), 
    verbatimTextOutput("summary"),
    tags$div(id = 'placeholder') 

)

server <- function(input, output, session) {
  ## keep track of elements inserted and not yet removed
  inserted <- c()

  observeEvent(input$insertBtn, {
    btn <- input$insertBtn
    id <- paste0('txt', btn)
    insertUI(
      selector = '#placeholder',
      ## wrap element in a div with id for ease of removal
      ui = tags$div(
        actionButton(inputId = paste0("truc",id),label = paste0("truc",id)), 
        id = id
      )
    )
    inserted <<- c(id, inserted)
  })

  observeEvent(input$removeBtn, {
    removeUI(
      ## pass in appropriate div id
      selector = paste0('#', inserted[length(inserted)])
    )
    inserted <<- inserted[-length(inserted)]
  })


  output$summary <- renderPrint({
    invalidateLater(1000)
    lst <- reactiveValuesToList(input) 
    message("upd")
    lst[order(names(lst))]
  })
}

shinyApp(ui, server)

知道怎么做吗?

【问题讨论】:

    标签: r shiny


    【解决方案1】:

    您可以使用shinyjs 修改输入。像这样的东西有用吗?

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
    
      actionButton('insertBtn', 'Insert'), 
      actionButton('removeBtn', 'Remove'), 
      verbatimTextOutput("summary"),
      tags$div(id = 'placeholder'), 
      useShinyjs()
    
    )
    
    server <- function(input, output, session) {
      ## keep track of elements inserted and not yet removed
      inserted <- c()
    
      observeEvent(input$insertBtn, {
        btn <- input$insertBtn
        id <- paste0('txt', btn)
        insertUI(
          selector = '#placeholder',
          ## wrap element in a div with id for ease of removal
          ui = tags$div(
            actionButton(inputId = paste0("truc",id),label = paste0("truc",id)), 
            id = id
          )
        )
        inserted <<- c(id, inserted)
      })
    
      observeEvent(input$removeBtn, {
        id <- inserted[length(inserted)]
        removeUI(
          ## pass in appropriate div id
          selector = paste0('#',id)
        )
        #use the javascript functio Shiny.onInputChange to set the values of 
        # the removed inputs to NULL, javascript uses lower case for null
        runjs(paste0("Shiny.onInputChange('",paste0("truc",id),"',null)"))
        inserted <<- inserted[-length(inserted)]
      })
    
    
      output$summary <- renderPrint({
        invalidateLater(1000)
        lst <- reactiveValuesToList(input) 
        message("upd")
        lst[order(names(lst))]
      })
    }
    
    shinyApp(ui, server)
    

    希望这会有所帮助!

    【讨论】:

    • 我使用这种代码:glue::glue("Shiny.onInputChange('{inp}',null)",inp=session$ns(names(reactiveValuesToList(input)))) %&gt;%walk(runjs) 它完成了这项工作。谢谢!
    【解决方案2】:

    如果您绝对想删除已创建的输入,我的解决方案将无济于事(但您仍然可以使用 Bertil Baron 的解决方案)。 但是,如果您只想清理输入(不删除它们),那么我建议这样做:

    library(shiny)
    
    ui <- fluidPage(
    
      actionButton('insertBtn', 'Insert'), 
      actionButton('removeBtn', 'Remove'), 
      verbatimTextOutput("summary"),
      tags$div(id = 'placeholder') 
    
    )
    
    server <- function(input, output, session) {
      ## keep track of elements inserted and not yet removed
      inserted <- reactiveVal(0)
    
      observeEvent(input$insertBtn, {
        inserted(inserted() + 1)
        id <- paste0('txt', inserted())
        insertUI(
          selector = '#placeholder',
          ## wrap element in a div with id for ease of removal
          ui = tags$div(
            actionButton(inputId = paste0("truc", id), label = paste0("truc", id)), 
            id = id
          )
        )
      })
    
      observeEvent(input$removeBtn, {
        removeUI(
          ## pass in appropriate div id
          selector = paste0('#txt', inserted())
        )
        inserted(inserted() - 1)
      })
    
    
      output$summary <- renderPrint({
        invalidateLater(1000)
        lst <- isolate(reactiveValuesToList(input) )
        message("upd")
        lst[order(names(lst))]
      })
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 2019-01-02
      • 2017-07-21
      • 1970-01-01
      • 2012-04-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-07
      • 1970-01-01
      相关资源
      最近更新 更多