如何在Shiny DT数据表中预选单元格

时间:2019-10-30 14:47:29

标签: r shiny dt

是否有一种方法可以预先选择闪亮的DT数据表中的单元格而不是行?

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')

    )
  ),
  server = function(input, output, session) {
    output$x1 = DT::renderDataTable(
      iris, server = FALSE,
      selection = list(mode = 'multiple', selected = c(1, 3, 8, 12),target="cell")
    )

  }
)

1 个答案:

答案 0 :(得分:2)

请参阅github指南,其中包含您所要查找的内容(关于此问题以及您最近发布的其他内容)。 https://rstudio.github.io/DT/shiny.html

  

2.1.4预选

     

datatable()的选择参数也可以包含一个组件   选择以指定要使用的行/列/单元格   在初始化表时预先选择。当target ='row'或   选择的“列”是行或列索引的向量。对于这种情况   target ='row + column'的列,应选择两个组成部分的列表   行和列,例如list(行= c(1,2,4,9),cols = c(1,3))。对于   target ='cell',它应该是两列的矩阵:第一列   column是选定单元格的行索引,第二列是   列索引。

要使其选择某个单元格,必须为其赋予坐标(行和列)。

library(DT)
shinyApp(
  ui = fluidPage(
    fluidRow(
      h1('Client-side processing'),
      DT::dataTableOutput('x1')

    )
  ),
  server = function(input, output, session) {
    output$x1 = DT::renderDataTable(
      iris, server = FALSE,
      selection = list(mode = 'multiple', selected = matrix(c(1, 3, 2, 4), nrow = 2, ncol = 3),target="cell")
    )

  }
)