Home > Net >  Activate the search filter when you put a value in a column full of NAs in a datatable
Activate the search filter when you put a value in a column full of NAs in a datatable

Time:07-25

I have a dataframe with a column full of NAs and I want to be able to fill those NAs but the search filter of the landman column still remains deactivated. Is there a way that I can activate it after putting values in?

library(shiny)
library(shinydashboard)
library(DT)
d<-structure(list(owner = c("7 MILL IRON RANCH LLC", "7/S LAND & CATTLE COMPANY LLC", 
                            "AHL/KENNETH L(TRUSTEE & JOHN E AHL ETAL"), acres = c(1900.6207117, 
                                                                                  654.7908393, 641.3866548), n = c(5L, 2L, 1L), landman = c(NA_character_, 
                                                                                                                                            NA_character_, NA_character_)), row.names = c(NA, -3L), class = c("tbl_df", 
                                                                                                                                                                                                              "tbl", "data.frame"))
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    dataTableOutput("TABLE"),
  )
)

server <- (function(input, output, session) {
  output$TABLE <- renderDataTable({
    datatable(d, filter = 'top', editable = 'cell', class='hover cell-border stripe', 
              caption = 'Owners wit more than 500 aggregated accrs',
              extensions = 'Buttons',
              options = list(dom = 'Bfrtip',
                             buttons = c('copy', 'csv', 'excel'))) 
  })
  
})

shinyApp(ui, server)

CodePudding user response:

We can save the new data to the object used to create the datable and force a re-render.

  1. Create reactive values. data <- reactiveValues(d = d)
  2. pass data$d to datatable data argument.
  3. Create an observer to capture the new modifications.
  observeEvent(input$TABLE_cell_edit, {
    edits <- input$TABLE_cell_edit
    data$d[edits$row, edits$col] <- edits$value
  })
})

APP:

library(shiny)
library(shinydashboard)
library(DT)
d<-structure(list(owner = c("7 MILL IRON RANCH LLC", "7/S LAND & CATTLE COMPANY LLC",
                            "AHL/KENNETH L(TRUSTEE & JOHN E AHL ETAL"), acres = c(1900.6207117,
                                                                                  654.7908393, 641.3866548), n = c(5L, 2L, 1L), landman = c(NA_character_,
                                                                                                                                            NA_character_, NA_character_)), row.names = c(NA, -3L), class = c("tbl_df",
                                                                                                                                                                                                              "tbl", "data.frame"))
ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(
    actionButton('browse', "Activate Browser"),
    dataTableOutput("TABLE"),
  )
)

server <- (function(input, output, session) {
  data <- reactiveValues(d = d)

  output$TABLE <- renderDataTable({
    datatable(
      data       = data$d,
      filter     = "top",
      editable   = "cell",
      class      = "hover cell-border stripe",
      caption    = "Owners wit more than 500 aggregated accrs",
      extensions = "Buttons",
      options    = list(
        dom     = "Bfrtip",
        buttons = c("copy", "csv", "excel")
      )
    )
  })


  observeEvent(input$TABLE_cell_edit, {
    edits <- input$TABLE_cell_edit
    data$d[edits$row, edits$col] <- edits$value
  })
})

shinyApp(ui, server)
  • Related