user51462
user51462

Reputation: 2022

R Shiny - Pre-selection of rows in Select extension for Datatables

How can I pre-select rows with the Select extension for Datatables in Shiny? I've checked the documentation here: https://datatables.net/extensions/select/. But I can't figure it out. I tried specifying rows = 1:3 but that didn't have any effect:

library(DT)
library(shiny)

dat <- iris[1:17,]

shinyApp(
  ui = fluidPage(DTOutput("table")),

  server = function(input, output, session) {  

    output[["table"]] <- renderDT({
        datatable(
          dat,
          options = list(select = list(
            style = "multi", 
            rows = 1:3, 
            selector = "td:not(.notselectable)")),
          extensions = "Select", selection = "none")
    }, server = FALSE)

  }
)

NB. I am using the Select extension for Datatables, not the DT package's implementation of row selection. So selection = list(selected = 1:3) will not work.

Upvotes: 1

Views: 527

Answers (1)

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

There's no select.rows option. You can use a callback:

output[["table"]] <- renderDT({
  datatable(
    dat,
    callback = JS("table.rows([0,1,2]).select();"),
    options = list(select = list(
      style = "multi", 
      selector = "td:not(.notselectable)")),
    extensions = "Select", selection = "none")
}, server = FALSE)

Upvotes: 2

Related Questions