rshinydt

DataTable allow selection of certain cells only


The following app allows the user to click in the table and select cells. Is there a possibility to restrict certain columns from being selected? For example, I would like the user to only be able to click within the Species and the Sepal.Length columns.

library(shiny)
library(DT)

ui <- fluidPage(
  DTOutput("dt"),
  verbatimTextOutput("txt")
)

server <- function(input, output, session) {
  output$dt <- renderDT({
    datatable(iris,
              selection=list(mode="single", target="cell"))
  })
  
  output$txt <- renderText({
    req(input$dt_cell_clicked)
    
    input$dt_cell_clicked$value
  })
}

shinyApp(ui, server)

Solution

  • If you go with

    selection = list(mode="single", target="cell", 
                     selectable = rbind(cbind(1:nrow(iris), rep(5, nrow(iris))),
                                        cbind(1:nrow(iris), rep(1, nrow(iris))))))
    

    and use input$dt_cells_selected (clicked != selected) instead it works fine.

    The matrix in selectable is a two-column matrix of all cells (row-column-pairs) that should be selectable.