Home > Net >  Update a dataset after putting a new value in the dt::datatable
Update a dataset after putting a new value in the dt::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 to update the dataset I use every time I put a value in the landman column and use a submitButton to update it?

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"
))

#d$landman <- letters[1:3]

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(
    selectInput("sel","select",choices = d$owner,multiple=T,selected = d$owner[1])
  ),
  dashboardBody(
    dataTableOutput("TABLE"),
  )
)

server <- (function(input, output, session) {
  observeEvent(input$TABLE_cell_edit, {
    d <<- editData(d, input$TABLE_cell_edit, 'TABLE')
  })
  
  output$TABLE <- renderDataTable({
    input$TABLE_cell_edit
    
    d<-subset(d,d$owner%in%input$sel)
    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:

As far as I get it the filter for the datatable columns gets deactivated when a column contains only one value, i.e. that's not specific to NAs. The same occurs if you replace the NAs by empty strings or e.g. an a. Unfortunately I wasn't able to find anything on this behavior in the documentation.

However, to activate the filter after you edited a cell you could follow the enter image description here

  • Related