How to pre-select cells in Shiny DT datatables

≡放荡痞女 提交于 2019-12-25 01:16:53

问题


Is there a way to pre-select cells in shiny DT datatables instead of rows?

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:


Please refer to the github guide which has exactly what you are looking for (in this question and others you have posted recently). 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.

In order for it to select a certain cell, you have to give it coordinates (row and 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")
    )

  }
)


来源:https://stackoverflow.com/questions/58627988/how-to-pre-select-cells-in-shiny-dt-datatables

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!