【问题标题】:Button not working for the second time in DT table shiny按钮在 DT 表闪亮中第二次不起作用
【发布时间】:2022-12-10 06:55:52
【问题描述】:

在下面的应用程序中,编辑第一次工作正常。 (假设你点击第一行按钮,它工作正常。但是一旦你取消并再次点击同一个按钮,它就不起作用了)

有人能帮我一下吗?它与绑定有关吗?

library(shiny)

ui <- fluidPage(
  dataTableOutput("df")
)

server <- function(input, output, session) {
  
  shinyInput <- function(FUN, len, id, ...) {
    inputs <- character(len)
    for (i in seq_len(len)) {
      inputs[i] <- as.character(FUN(paste0(id, i), ...))
    }
    inputs
  }
  
  output$df <- renderDataTable({
    iris$Edit = shinyInput(actionButton, nrow(iris), 'button_', label = "Edit", onclick = 'Shiny.onInputChange(\"select_button\",  this.id)' )
    datatable(iris, escape = F) 
  })

  observeEvent(input$select_button,{
     selectedRow <- as.numeric(strsplit(input$select_button, "_")[[1]][2])

    showModal(modalDialog(
      title = "Edit", size = "l",
      fluidPage(width = 12,fluidRow(
        column(width = 3,selectedRow)
      )),
      footer = tagList(modalButton("Cancel"),actionButton("update", "Update"))))
  })
}

shinyApp(ui, server)

【问题讨论】:

  • 在某处添加observe(print(input$select_button)),您应该明白为什么它只工作一次。 observeEvent(input$select_button...,{}) 仅在 input$select_button 值更改时触发。但是如果您在同一行上单击两次,它不会更改它的值,因为它的值是基于该行的。您需要找到另一种方法来定义我认为的按钮 ID。
  • 有办法处理这个吗??
  • 例如,我认为您可以在每次单击按钮时更改按钮的 ID。并且它的 id 看起来像 select_button1_0 如果从来没有点击过,select_button1_1 如果点击一次等等......这样你仍然可以用 str_split 得到行号,如果你点击第二次,input$select_button1_X 不会是与上次点击完全相同的 ID。
  • 我懂了。你能展示一个样品吗
  • 我很想尝试,但我根本不懂 javascript。我无法找到连接“this.id”和“_0”的代码片段(我搜索了但不是很多啊哈)。

标签: shiny


【解决方案1】:

你应该把this.id换成this.id + "_" + Date.now() 当您生成唯一的 this.id 时,它可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    • 2020-10-28
    相关资源
    最近更新 更多