如何在Shiny DT数据表中预选行

sre*_*ion 6 javascript datatable jquery r shiny

我正在使用Shiny(0.12.0)和DT(0.0.65)在这个Shiny数据表中进行行选择.我想预先选择前5行.我试过了:

  • callback在datatable中使用JS 更改行的类.但是,这并没有反映在input$x1_rows_selected变量中.由于CSS,只有背景/突出显示发生变化.
  • 使用.click()任一rowCallback选项列表或callback.这在加载页面时不起作用.但是,input$x1_rows_selected当我通过控制台/浏览器开发工具运行相同的代码时,它可以工作(更新).

callback JS:

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

Yih*_*Xie 7

此功能已添加到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)])
    )
  }
)
Run Code Online (Sandbox Code Playgroud)

  • @ClaudH这是文档:http://rstudio.github.io/DT/server.html (2认同)