【问题标题】:Remove actionButton() via removeUI() function in shiny通过闪亮的 removeUI() 函数删除 actionButton()
【发布时间】:2018-09-07 02:48:44
【问题描述】:

我正在尝试使用闪亮包中的removeUI() 函数,但我在删除特定元素时遇到了一些困难。下面是这个问题的一个例子,根据help(removeUI) 的例子稍加修改。具体来说,包含textInput( 的注释行被替换为actionButton(。

library(shiny)

ui <- fluidPage(
    sidebarPanel(
      actionButton("rmv", "Remove UI"),
      actionButton("txt", "This is no longer useful")
      #textInput("txt", "This is no longer useful")
    )
)

# Server logic
server <- function(input, output, session) {
  observeEvent(input$rmv, {
    removeUI(
      selector = "div:has(> #txt)"
    )
  })
}

# Complete app with UI and server components
shinyApp(ui, server)

虽然textInput() 的组件可以删除,但此方法不适用于使用actionButton() 删除类似组件。我不确定为什么,因为在这种情况下 jQuery 选择器的逻辑看起来是一样的。

我还尝试将 selector = "div:has(&gt; #txt)" 更改为 selector = "button:has(&gt; #txt)",认为 jQuery 搜索将适用于 button 元素,但同样无济于事。

编辑:找到解决方案。用div() 包裹actionButton("txt", "This is no longer useful") 会将操作按钮放入div&lt;&gt; 块中,因此搜索将起作用。

【问题讨论】:

    标签: r shiny


    【解决方案1】:
      1234563此规则适用于网络上的所有 HTML 页面。
    • 关于这个问题,我写了一个小函数,可以让你收集匹配特定模式的元素


    library(shiny)
    
    ui <- fluidPage(
      sidebarPanel(
        actionButton("rmv", "Remove UI"),
        actionButton("txt", "This is no longer useful"),
        textInput("txt2", "This is no longer useful")
      )
    )
    
    # Server logic
    server <- function(input, output, session) {
    
      getInputs <- function(pattern){
        reactives <- names(reactiveValuesToList(input))
        reactives[grep(pattern,reactives)]
      }
    
      observeEvent(input$rmv, {
    
        vals <- getInputs("txt")
        vals <- paste0("#",vals)
    
        removeUI(
          selector = vals,
          multiple = T
        )
      })
    }
    # Complete app with UI and server components
    shinyApp(ui, server)
    

    【讨论】:

    • 谢谢。我不知道reactiveValuesToList() 函数。这就是我找到输入名称所需要的。
    猜你喜欢
    • 2020-01-18
    • 2020-04-03
    • 1970-01-01
    • 2016-02-20
    • 2021-04-20
    • 2018-10-25
    • 2022-12-10
    • 2017-04-10
    • 1970-01-01
    相关资源
    最近更新 更多