vendredi 22 février 2019

Register shiny checkboxInput value on DT

I am able to embed checkbox in each cell of a DT column from reading this post.

Imagine I have a dataframe with a logical column named "value" containing some TRUE values, and I want the checkbox in "value_check" column to appear as checked for those that are TRUE upon app start, like shown below: How would I do so?

enter image description here

library(shiny)
library(DT)

df <- data.frame(item = c("a", "b", "c"), value = c(TRUE, NA, NA))

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
}

## obtaining checkbox value
shinyValue = function(id, len) { 
  unlist(lapply(seq_len(len), function(i) { 
    value = input[[paste0(id, i)]] 
    if (is.null(value)) FALSE else value 
  })) 
} 

server <- function(input, output, session) {
  output$tbl <- renderDT(server = FALSE, escape = FALSE, editable = TRUE, options = list(
    dom = 't', paging = FALSE, ordering = FALSE,
    preDrawCallback = JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
    drawCallback = JS('function() { Shiny.bindAll(this.api().table().node()); } ')
  ), {
    df$value_check <- shinyInput(checkboxInput, nrow(df), "check")
    df
    }
  )
}

ui <- fluidPage(
  DTOutput("tbl")
)

shinyApp(ui, server)




Aucun commentaire:

Enregistrer un commentaire