【问题标题】:How to pre-select rows in Shiny DT datatables如何在 Shiny DT 数据表中预选行
【发布时间】:2015-08-25 02:44:25
【问题描述】:

我在 Shiny datatable 中使用 Shiny (0.12.0) 和 DT (0.0.65) 进行行选择。我想预先选择前 5 行。我试过了:

  • 在数据表中使用callbackJS 更改行的类。但是,这并没有反映在 input$x1_rows_selected 变量中。由于 CSS,只有背景/高光会发生变化。
  • 在选项列表中的rowCallbackcallback 中使用.click()。这在加载页面时也不起作用。但是,当我通过控制台/浏览器开发工具运行相同的代码时,它可以工作(更新 input$x1_rows_selected)。

callbackJS:

output$x1 = DT::renderDataTable({
    datatable(cars,
        rows = $("#x1 tbody tr");
        $(rows).slice(0,5).each(function() {
            $(this).click();
        });
    )
})

【问题讨论】:

  • 仅作记录,在github.com/rstudio/DT/issues/89 中发布并在github.com/rstudio/DT/issues/93 中提到的相同问题我会看看我能做些什么。
  • 谢谢你的工作。虽然对于客户端,我必须这样做:selection = list(mode = 'multiple', selected = as.character(c(1,3,8)) 而不仅仅是数字。如果您可以发表您的评论作为答案,我可以接受。

标签: javascript jquery r datatable shiny


【解决方案1】:

此功能已添加到 DT (>= 0.1.3)。例子:

library(shiny)
if (packageVersion('DT') < '0.1.3') devtools::install_github('rstudio/DT')
library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
      h1('Client-side processing'),
      DT::dataTableOutput('x1'),
      h1('Server-side processing'),
      DT::dataTableOutput('x2')
    )
  ),
  server = function(input, output, session) {
    output$x1 = DT::renderDataTable(
      iris, server = FALSE,
      selection = list(mode = 'multiple', selected = c(1, 3, 8, 12))
    )
    output$x2 = DT::renderDataTable(
      iris, server = TRUE,
      selection = list(mode = 'multiple', selected = rownames(iris)[c(1, 3, 8, 12)])
    )
  }
)

【讨论】:

  • 嗨!你能解释一下客户端和服务器端处理之间的区别吗?它如何影响任何事情?
  • @ClaudH 这是文档:rstudio.github.io/DT/server.html
  • 是否可以将反应输入绑定到所选参数?我试过但没有工作selection = list(mode = 'multiple', selected = input$s)
猜你喜欢
  • 1970-01-01
  • 2015-06-29
  • 2015-06-29
  • 2016-05-12
  • 2018-06-22
  • 2018-07-02
  • 2020-03-15
  • 2017-01-01
  • 2019-08-25
相关资源
最近更新 更多