DataTables DT:单击单元格的重置值

DataTables DT: reset value of clicked cell

我想添加点击 table 单元格后发生的事情的功能(例如打开模式)。因为(假设我的 dt 具有 ID "dt")input$dt_cell_clicked 在我单击一个新单元格之前保持不变,我无法在重新单击该单元格时执行相同的事件。

我尝试解决此问题,手动将 input$dt_cell_clicked 重置为 javascript。这有效,但 DT 中似乎有一个内部更新标记注意到我之前单击了单元格并且没有将 input$dt_cell_clicked 的值设置为单击的值。有解决方法还是这是一个错误?

谢谢!

最小示例:

library(shiny)
library(shinyjs)

ui <- fluidPage(
  h2("Last clicked:"),
  verbatimTextOutput("last_clicked"),
  actionButton("reset", "Reset clicked value"),
  h2("Datatable:"),
  DT::dataTableOutput("dt"),
  useShinyjs(),
  extendShinyjs(text = paste0("shinyjs.resetDTClick = function() { Shiny.onInputChange('dt_cell_clicked', null); }"))
)

server <- function(input, output) {

  # the last clicke value
  output$last_clicked <- renderPrint({
    str(input$dt_cell_clicked)
  })

  output$dt <- DT::renderDataTable({
    DT::datatable(head(mtcars, 2))
  })

  observeEvent(input$dt_cell_clicked, {
    validate(need(length(input$dt_cell_clicked) > 0, ''))
    alert("You clicked something!")
  })

  observeEvent(input$reset, {
    js$resetDTClick()
  })
}

shinyApp(ui, server)

这是一个可以正确重置的版本。它使用一个反应值(称为 last),只要按下重置按钮就会设置为 NULL,并在更新此值时采用 input$dt_cell_clicked 的值。

我还通过使用 dataTableProxyselectRows

shinyjs 中删除了依赖项
library(shiny)

ui <- fluidPage(
  h2("Last clicked:"),
  verbatimTextOutput("last_clicked"),
  actionButton("reset", "Reset clicked value"),
  h2("Datatable:"),
  DT::dataTableOutput("dt")
)

server <- function(input, output) {

  # the last clicke value
  output$last_clicked <- renderPrint({
    str(last())
  })

  output$dt <- DT::renderDataTable({
    DT::datatable(head(mtcars, 2))
  })

  observeEvent(input$dt_cell_clicked, {
    validate(need(length(input$dt_cell_clicked) > 0, ''))
    print("You clicked something!")
  })

  myProxy = DT::dataTableProxy('dt')
  last = reactiveVal(NULL)

  observe({
    last(input$dt_cell_clicked)
  })

  observeEvent(input$reset, {
    DT::selectRows(myProxy, NULL)
    last(NULL)
    output$dt <- DT::renderDataTable({    # EDIT
      DT::datatable(head(mtcars, 2))      # EDIT
    })                                    # EDIT
  })
}

shinyApp(ui, server)

在提示使用代理 (Gregor de Cillia) 后,我找到了一个使用绕道的解决方法:最后选择的单元格。观察最后选择的单元格,并根据新选择执行某些操作。我们可以使用 DT 代理重置选择。

library(shiny)

ui <- fluidPage(
  h2("Last clicked:"),
  verbatimTextOutput("last_clicked"),
  actionButton("reset", "Reset clicked value"),
  h2("Datatable:"),
  DT::dataTableOutput("dt")
)

server <- function(input, output) {

  # the last clicked=selected value
  output$last_clicked <- renderPrint({
    str(input$dt_rows_selected)
  })

  output$dt <- DT::renderDataTable({
    DT::datatable(head(mtcars, 2), selection = 'single')
  })

  # do some action after selecting a value
  observeEvent(input$dt_rows_selected, {
    validate(need(!is.null(input$dt_rows_selected), ''))
    print("You clicked something!")
  })

  myProxy = DT::dataTableProxy('dt')

  # reset last selected value using the proxy
  observeEvent(input$reset, {
    DT::selectRows(myProxy, NULL)
  })
}

shinyApp(ui, server)