代码之家  ›  专栏  ›  技术社区  ›  thothal

使用removeUI时删除相应的输入元素

  •  1
  • thothal  · 技术社区  · 8 年前

    问题

    如何删除/作废对应的 input 元素,删除控件时 removeUI ?

    你可以在 reprex 即使在那之后 textInput 被删除, input$x 仍然是“真理”。理想情况下,我可以说 shiny 那个 输入$x 不再有效 reactives 依靠 输入$x 把它当作空的。

    更新

    读到目前为止的答案,我想我还不清楚,我想实现什么。我真的想知道一个人是否可以在概念上“作废” 输入$x . 在这种情况下,我不必担心会有什么东西打破界限。


    雷普雷克斯

    library(shiny)
    
    ui <- fluidPage(
      div(textInput("x", "Text"), id = "killme"),
      actionButton("del", "Delete"),
      verbatimTextOutput("out")
    )
    
    server <- function(input, output) {
      output$out <- renderPrint(req(input$x))
      observeEvent(input$del, removeUI("#killme"))
    }
    
    shinyApp(ui, server)
    
    2 回复  |  直到 8 年前
        1
  •  1
  •   SeGa    8 年前

    你可以用 reactiveValues 对于打印输出,并在删除div时将其指定为空。我不知道是否有更优雅的解决方案。

    library(shiny)
    
    ui <- fluidPage(
      div(textInput("x", "Text"), id = "killme"),
      actionButton("del", "Delete"),
      verbatimTextOutput("out")
    )
    
    server <- function(input, output) {
      textX <- reactiveValues(x = NULL)
    
      observe({
        textX$x = input$x
      })
    
      observeEvent(input$del, {
        textX$x = NULL
        removeUI("#killme")
        })
    
      output$out <- renderPrint({
        req(textX$x)
        textX$x
        })
    }
    
    shinyApp(ui, server)
    
        2
  •  1
  •   thothal    8 年前

    这个 SO question ,显示如何使输入无效。这样,解决方案变成:

    ui <- fluidPage(
      tags$script("
        Shiny.addCustomMessageHandler('resetValue', function(variableName) {
          Shiny.onInputChange(variableName, null);
        });
      "),
      div(textInput("x", "Text"), id = "killme"),
      actionButton("del", "Delete"),
      verbatimTextOutput("out")
    )
    
    server <- function(input, output, session) {
      output$out <- renderPrint(req(input$x))
      observeEvent(input$del, {
        removeUI("#killme")
        session$sendCustomMessage("resetValue", "x")})
    
    }
    
    shinyApp(ui, server)
    

    不过,谢谢你的回答,因为只有通过讨论,我才能使我的问题更清楚,并能找到正确的解决办法。


    简式使用 library(shinyjs) :

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
      useShinyjs(debug = TRUE),
      div(textInput("x", "Text"), id = "killme"),
      actionButton("del", "Delete"),
      verbatimTextOutput("out")
    )
    
    server <- function(input, output, session) {
      output$out <- renderPrint(req(input$x))
      observeEvent(input$del, {
        removeUI("#killme")
        runjs('Shiny.onInputChange("x", null)')
      })
    
    }
    
    shinyApp(ui, server)