How to select the particular value in the data table

Solution 1:

Tested:

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

ui <- shinyUI(fluidRow(
    DT::dataTableOutput("myDatatable"),
    verbatimTextOutput("selectedCells")
))
df <- cbind(new_name =rownames(mtcars), data.frame(mtcars, row.names= NULL))
server <- shinyServer(function(input, output, session) {
    output$myDatatable <- DT::renderDataTable(
        df, extensions = 'ColReorder', options = list(colReorder = TRUE),selection = list(mode = "single", target ="cell"),
        server = FALSE,
        rownames = T
    )
    
    output$selectedCells <- renderPrint({
        
        s_val = input$myDatatable_cell_clicked$value
        s = input$myDatatable_cells_selected
        if (!is.null(s) && ncol(s) != 0) {
            df[df$new_name==s_val,]
            
        } else {
            NULL
        }
    })
})

shinyApp(ui, server)