[英]How to pre-select rows in Shiny DT datatables
我正在使用Shiny(0.12.0)和DT(0.0.65)在這個Shiny數據表中進行行選擇。 我想預先選擇前5行。 我試過了:
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();
});
)
})
此功能已添加到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)])
)
}
)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.