How to read a cell value from an edited dataframe

I'm quite new to R and Shiny so this might be quite basic but I've searched for hours and can't find an answer.

The below (very basic) example renders an editable table and prints out the value of cell [4,1] from the source dataframe below the rendered table. How can I print out the value of cell [4,1] from the editable datatable? (i.e., so that if someone changes the table value, the value printed also changes).

The "Using DT in Shiny" help page states that "after you finish editing [a cell], you can obtain the row/column indices and the new values of the cells that were edited via input$tableId_cell_info", however I can't find any more info on how to use this feature.

Any help would be much appreciated, thanks!

library(shiny)
library(DT)

shinyApp(
  
  ui = fluidPage(fluidRow(column(offset = 1,
                                 width = 6,
                                 DTOutput('tbl'))),
                 hr(),
                 fluidRow(column(offset = 1,
                                 width = 6,
                                 uiOutput('print_cell')))),
  
  server = function(input, output) {
    
    tab <- data.frame(x = c(1,2,3,4),
                      y = c(5,6,7,8))    
    
    output$tbl = renderDT(
      datatable(tab,
                rownames = FALSE,
                editable = TRUE))
    
    output$print_cell <- renderUI(tab[4,1])
    
  }
)```

Hi @Dylan_Edgar ,

i hope this helps

library(shiny)
library(DT)

shinyApp(
  
  ui = fluidPage(fluidRow(column(offset = 1,
                                 width = 6,
                                 DTOutput('tbl'))),
                 hr(),
                 fluidRow(column(offset = 1,
                                 width = 6,
                                 uiOutput('print_cell')))),
  
  server = function(input, output) {
    
    tab <- data.frame(x = c(1,2,3,4),
                      y = c(5,6,7,8)) 
    

    
    output$tbl = renderDT(
      datatable(tab,
                rownames = FALSE,
                editable = TRUE))
    
    show41 <- reactiveVal(tab[4,1])
    output$print_cell <- renderUI({
      # stored in temp variable
      info <- input$tbl_cell_edit
      # initially this will be NULL since nothing has been edited so just use the preset value
      if(is.null(info)){
        show41()
      # update value based on change in table
      }else if(info$row == 4 & info$col == 0){
        show41(info$value)
        show41()
      # updated some other value in the table so just show the current value show41 has
      }else{
        show41()
      }
    })
    
  }
)

Perfect. Thank you very much!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.