Adam Spannbauer Adam Spannbauer - 3 months ago 38
R Question

Shiny + DT: Single Cell Selection

I am trying to use the development version of the DT package (available at

devtools::install_github('rstudio/DT')
) to enable single cell selection in a shiny application. I have been able to make the selection be cells using the
target
argument for
selection
; however, I can't figure out how to disable multiple cells being selected. Is there another argument for the
selection
parameter list that will allow me to restrict the user's selection to a max of 1 cell? If not, is there another way to accomplish single cell selection?

I am very open to revert back to the stable version of
DT
on CRAN if there is an easier solution using that version of the package.

library(shiny)
library(DT)
data("mtcars")

ui <- shinyUI(
fluidRow(
DT::dataTableOutput("myDatatable"),
verbatimTextOutput("selectedCells")
)
)

server <- shinyServer(function(input, output, session) {
output$myDatatable <- DT::renderDataTable(mtcars,
selection=list(target="cell"),
server = FALSE,
rownames=FALSE)

output$selectedCells <- renderPrint(input$myDatatable_cells_selected)
})

shinyApp(ui, server)

Answer

The problem is in your call to DT::renderDataTable in the selection list. You need selection=list(mode="single", target="cell")

mode sets single or multiple where you had selection (before your edit)