Avioli
Avioli

Reputation: 39

Get data of selected cell using Data Tables in shiny

I have a data table in Shiny where I need to get the data out of the cell in order to display the proper output.

By using input$tableId_cells_selected, I am able to retrieve the location of the selected cell in the table. This is helpful, however I also need to reference what is actually in the cell to write an output function.

I found this link which may be helpful, but I was not able to apply the functionality to actually work in my Shiny server function.

Any help is appreciated.

Upvotes: 2

Views: 3479

Answers (1)

Mal_a
Mal_a

Reputation: 3760

Here is solution for you:

library(shiny)
library(DT)
shinyApp(
  ui = fluidPage(DT::dataTableOutput('tableId'),
                 textOutput("celltext")),
  server = function(input, output) {
    output$tableId = DT::renderDataTable(
      iris, , selection = list(target = 'cell')
    )

    output$celltext <- renderText({
      cell <- input$tableId_cells_selected
      iris <- iris[cell]
    })
  }
)

with textOutput below data table you can see the values of the selected cell...

The only thing which you need to do is to use the input$tableId_cells_selected argument to subset the data:

cell <- input$tableId_cells_selected
iris <- iris[cell]

Next time please post reproducible example!

Upvotes: 8

Related Questions