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

How to pre-select cells in Shiny DT datatables

有没有办法在闪亮的 DT 数据表中预先 select 单元格而不是行?

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

  }
)

请参阅 github 指南,它正是您要查找的内容(在这个问题和您最近发布的其他问题中)。 https://rstudio.github.io/DT/shiny.html

2.1.4 Pre-selection

The selection argument of datatable() can also include a component selected to specify which rows/columns/cells to be pre-selected when the table is initialized. When target = 'row' or 'column', selected is a vector of row or column indices. For the case of target = 'row+column', selected should be a list of two components rows and cols, e.g. list(rows = c(1, 2, 4, 9), cols = c(1, 3)). For target = 'cell', it should be a matrix of two columns: the first column is the row indices of selected cells, and the second column is the column indices.

为了select某个单元格,你必须给它坐标(行和列)。

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

  }
)