Home > Back-end >  Subset a dataframe by single cell selection of DT::datatable
Subset a dataframe by single cell selection of DT::datatable

Time:07-23

Below Im trying to subset the second datatable by clicking on a cell of the column owners of the fisrt datatable.

## app.R ##
library(shiny)
library(shinydashboard)

library(DT)
ckall<-structure(list(rowid = c(5704, 10243, 10116), PID = c("170040240", 
                                                      "170039927", "170184811"), county = c("Cheyenne", "Cheyenne", 
                                                                                            "Cheyenne"), ownerID = c(526, 526, NA), owner = c("JAMES A (TRUSTEE RUNDSTROM", 
                                                                                                                                              "JAMES A (TRUSTEE RUNDSTROM", "UNITED METHODIST CHURCH")), row.names = c(NA, 
                                                                                                                                                                                                                       -3L), class = c("tbl_df", "tbl", "data.frame"))
 

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    
  ),
  dashboardBody(
    dataTableOutput("TABLE"),
    dataTableOutput("TABLE2"),
    
  )
)
server<-(function(input, output, session) {
  
  
  
  output$TABLE<-renderDataTable({
    server = TRUE
    selection=list(target='cell')
    datatable(ckall, filter = 'top', editable = 'cell', class='hover cell-border stripe',
              caption = 'Kimball & Cheyenne Counties AOI',
              extensions = 'Buttons',
              options = list(dom = 'Bfrtip',scrollX=T,
                             buttons = c('copy', 'csv', 'excel')))
  })
  output$TABLE2 = DT::renderDataTable({
    req(input$TABLE_cells_selected)
    ckall[input$TABLE_cells_selected[,1], input$TABLE_cells_selected[,2], drop = F]
  })
  
})

shinyApp(ui, server)

CodePudding user response:

To make your code work you have to add selection = list(target = "cell") as an argument of your datatable call. The way you used it inside {} you create a variable with name selection which however has no effect on the datatable:

library(shiny)
library(shinydashboard)
library(DT)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    dataTableOutput("TABLE"),
    dataTableOutput("TABLE2"),
  )
)
server <- (function(input, output, session) {
  output$TABLE <- renderDataTable({
    server <- TRUE
    datatable(ckall,
      filter = "top", editable = "cell", class = "hover cell-border stripe",
      caption = "Kimball & Cheyenne Counties AOI",
      extensions = "Buttons",
      options = list(
        dom = "Bfrtip", scrollX = T,
        buttons = c("copy", "csv", "excel")
      ),
      selection = list(target = "cell"),
    )
  })
  output$TABLE2 <- DT::renderDataTable({
    req(input$TABLE_cells_selected)
    ckall[input$TABLE_cells_selected[, 1], input$TABLE_cells_selected[, 2], drop = F]
  })
})

shinyApp(ui, server)

enter image description here

  • Related