如何在 Shiny DT 数据表中预先选择行 [英] How to pre-select rows in Shiny DT datatables

查看:34
本文介绍了如何在 Shiny DT 数据表中预先选择行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个 闪亮的数据表.我想预先选择前 5 行.我试过了:

I'm using Shiny (0.12.0) with DT (0.0.65) for row-selections in this Shiny datatable. I want to pre-select the first 5 rows. I have tried:

  • 在数据表中使用 callback JS 更改行的类.但是,这并未反映在 input$x1_rows_selected 变量中.由于 CSS,只有背景/突出显示会发生变化.
  • 在选项列表中的 rowCallbackcallback 中使用 .click().这在加载页面时也不起作用.但是,当我通过控制台/浏览器开发工具运行相同的代码时,它可以工作(更新 input$x1_rows_selected).
  • Changing the class of row using callback JS in datatable. However, that is not reflecting in the input$x1_rows_selected variable. Only the background/highlight changes because of CSS.
  • Using .click() in either rowCallback in the options list or in callback. This does not work either when loading the page. However, it works (updates input$x1_rows_selected) when I run the same code through the console / browser dev tool.

回调 JS:

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

推荐答案

此功能已添加到 DT (>= 0.1.3).示例:

This feature has been added to DT (>= 0.1.3). Examples:

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 DT 数据表中预先选择行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆