gpt4 book ai didi

javascript - 如何预选 Shiny DT 数据表中的行

转载 作者:数据小太阳 更新时间:2023-10-29 05:15:13 25 4
gpt4 key购买 nike

我在这个 Shiny datatable 中使用带有 DT (0.0.65) 的 Shiny (0.12.0) 进行行选择.我想预选前 5 行。我试过:

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

回调 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)])
)
}
)

关于javascript - 如何预选 Shiny DT 数据表中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30746325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com