问题
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