2

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.

zx8754
  • 52,746
  • 12
  • 114
  • 209
Avioli
  • 39
  • 1
  • 5

1 Answers1

8

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!

Mal_a
  • 3,670
  • 1
  • 27
  • 60
  • thanks for your repsonse but I don't think I was thorough enough in my explanation as to what I'm looking for. I don't need to change anything in my UI and I don't want to output the contents of the cell. I just need a variable that stores what is in the cell so I can use that in a function. So if we know the input$tableID_cells_selected, how do I access what is actually in the cell? Thanks – Avioli Aug 30 '17 at 07:28
  • Well i did actually posted in my answer the output of the cell: `value<- reactive({iris[input$tableId_cells_selected])}` ...And you can use it further in your function by just typing: `value()` – Mal_a Aug 30 '17 at 08:19
  • Mal_a, I appreciate all of your answers but the table I'm getting the data from is also reactive. So its not as simple as pulling from the same set of data (iris) that populates the table. There are 36 different data frames that could populate the table depending on the users selection of input. – Avioli Aug 30 '17 at 11:41
  • i hope you have noticed how reproducible example is important in this case. Assuming you have not added any of your code/data, my answer is fully correct and working...I cannot see any other way to help you, or any other person in this forum without reproducible example. If your data frame is reactive whats the problem to use instead of iris --> `reactive_data()[cell]`? We can just argue now and assume thousands of different things, but your question was just: how to retrieve data from the selected cell. – Mal_a Aug 30 '17 at 11:42
  • I apologize for not being more clear. It would've been difficult to provide reproducible code because there are a lot of different functions involved and I'm not sure how helpful it would've been considering how many different data sets are involved in this data table. I was just hoping there was a simple command to retrieve data from the current selected cells in the table. No worries, I'll search for a solution elsewhere. have a nice day, thank you for trying to help. – Avioli Aug 30 '17 at 12:04
  • well if your reactive data frame has a name then you can easily use: `reactive_data()[input$tableId_cells_selected]` – Mal_a Aug 30 '17 at 12:05