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

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

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

callback JS:

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

此function已添加到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)]) ) } )