0

I'm using am older thread to reference a solution and can't figure out how to custom set T/F on a checkbox column in DT.

Is there a way to set all checkboxes to all True or even a combination of True False? The setting at dat1 is the issue

library(shiny)
library(DT)

ui <- fluidPage(
  br(),
  fluidRow(
    column(
      6,
      DTOutput("dtable")
    ),
    column(
      6,
      verbatimTextOutput("reactiveDF")
    )
  )
)

shinyInput <- function(FUN, len, id, ...) {
  inputs <- character(len)
  for (i in seq_len(len)) {
    inputs[i] <- as.character(FUN(paste0(id, i), label = NULL, ...))
  }
  inputs
}

dat0 <- data.frame(
  fruit  = c("apple", "cherry", "pineapple", "pear"),
  letter = c("a", "b", "c", "d")
)

dat1 <- cbind(dat0, bool = TRUE) #<-Main Issue, any change here is not picked up at startup

dat2 <- cbind(
  dat0,
  check = shinyInput(checkboxInput, nrow(dat0), "checkb")
)

js <- c(
  "$('[id^=checkb]').on('click', function(){",
  "  var id = this.getAttribute('id');",
  "  var i = parseInt(/checkb(\\d+)/.exec(id)[1]);",
  "  var value = $(this).prop('checked');",
  "  var info = [{row: i, col: 3, value: value}];",
  "  Shiny.setInputValue('dtable_cell_edit:DT.cellInfo', info);",
  "})"
)

server <- function(input, output, session) {

  Dat <- reactiveVal(dat1)

  output[["dtable"]] <- renderDT({
    datatable(
      dat2, 
      rownames = TRUE,
      escape = FALSE,
      editable = list(target = "cell", disable = list(columns = 3)),
      selection = "none",
      callback = JS(js)
    )
  }, server = FALSE)

  observeEvent(input[["dtable_cell_edit"]], { 
    info <- input[["dtable_cell_edit"]] # this input contains the info of the edit
    print(info)
    Dat(editData(Dat(), info))
  })
  
  output[["reactiveDF"]] <- renderPrint({ 
    Dat()
  })

}

shinyApp(ui, server)

In below thread, it's the 'check' column

How to add checkbox in datatable in a shiny module?

Thanks

LBZR
  • 161
  • 12
  • Please show us what you have tried so far. – lz100 Oct 26 '22 at 23:55
  • Hi, sorry that I didn't share the code. Updated. The main issue is the dat1 dataframe which has all TRUE in the column is overlooked by dat2 while it creates the check column. Is there a way shinyInput/checkboxInput can consider what set of bool values were provided and set them accordingly at startup? – LBZR Oct 28 '22 at 12:03

0 Answers0