Reputation: 8404
I would like to extract the value of a selected cell instead of its row and column coordinates when I click on it as I want to use it as input for another procedure.
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(mode="single", target="cell"),
server = FALSE,
rownames=FALSE)
output$selectedCells <- renderPrint(input$myDatatable_cells_selected)
})
shinyApp(ui, server)
Upvotes: 0
Views: 491
Reputation: 694
You can access the value in the table with row and column number like the following:
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(mode = "single", target =
"cell"),
server = FALSE,
rownames = FALSE
)
output$selectedCells <- renderPrint({
s = input$myDatatable_cells_selected
if (!is.null(s) && ncol(s) != 0) {
mtcars[s[1, 1] , s[1, 2] + 1]
} else {
NULL
}
})
})
shinyApp(ui, server)
As you can see, one must be added to column value to specify appropriate position. Handling unselected case is also important.
Upvotes: 1