如何在 Shiny DT 数据表中预 select 行
How to pre-select rows in Shiny DT datatables
我在此 Shiny datatable 中将 Shiny (0.12.0) 与 DT (0.0.65) 用于行-select离子。我想预 select 前 5 行。我试过:
- 正在使用数据表中的
callback
JS 更改行的 class。但是,这并没有反映在 input$x1_rows_selected
变量中。由于 CSS. 只有 background/highlight 发生了变化
- 在选项列表的
rowCallback
或 callback
中使用 .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();
});
)
})
此功能已添加到 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)])
)
}
)
我在此 Shiny datatable 中将 Shiny (0.12.0) 与 DT (0.0.65) 用于行-select离子。我想预 select 前 5 行。我试过:
- 正在使用数据表中的
callback
JS 更改行的 class。但是,这并没有反映在input$x1_rows_selected
变量中。由于 CSS. 只有 background/highlight 发生了变化
- 在选项列表的
rowCallback
或callback
中使用.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();
});
)
})
此功能已添加到 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)])
)
}
)