使用removeUI

时间:2018-06-23 14:43:52

标签: r shiny

在删除某些元素之后,如何清理“ input” (示例中的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)

有什么想法吗?

2 个答案:

答案 0 :(得分:4)

您可以使用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)

希望这会有所帮助!

答案 1 :(得分:0)

如果您绝对想删除自己创建的输入,我的解决方案将无济于事(尽管您仍然可以使用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)