4

i've read and implemented checkbox in table in shiny from link . but when i run in R, the output in column is <input type="checkbox" name="row1" value="1"> , <input type="checkbox" name="row2" value="2"> , etc in every "pick" cell, and i hope the output in "pick" column is checkbox, what the solution from my problem? thank you, this is the code

library(shiny)
mymtcars = mtcars
mymtcars$id = 1:nrow(mtcars)
runApp(
  list(ui = pageWithSidebar(
    headerPanel('Examples of DataTables'),
    sidebarPanel(
      checkboxGroupInput('show_vars', 'Columns to show:', names(mymtcars),
                         selected = names(mymtcars))
      ,textInput("collection_txt",label="Foo")
    ),
    mainPanel(
      dataTableOutput("mytable")
    )
  )
  , server = function(input, output, session) {
    rowSelect <- reactive({
      paste(sort(unique(input[["rows"]])),sep=',')
    })
    observe({
      updateTextInput(session, "collection_txt", value = rowSelect() ,label = "Foo:" )
    })
    output$mytable = renderDataTable({
      addCheckboxButtons <- paste0('<input type="checkbox" name="row', mymtcars$id, '" value="', mymtcars$id, '">',"")
      #Display table with checkbox buttons
      cbind(Pick=addCheckboxButtons, mymtcars[, input$show_vars, drop=FALSE])
    }, options = list(orderClasses = TRUE, lengthMenu = c(5, 25, 50), pageLength = 25)
    , callback = "function(table) {
    table.on('change.dt', 'tr td input:checkbox', function() {
    setTimeout(function () {
    Shiny.onInputChange('rows', $(this).add('tr td input:checkbox:checked').parent().siblings(':last-child').map(function() {
    return $(this).text();
    }).get())
    }, 10); 
    });
  }")
  }
  )
)
Community
  • 1
  • 1
lukman
  • 157
  • 1
  • 3
  • 10

1 Answers1

10

You can use DT with , escape = FALSE see

library(shiny)
library(DT)
mymtcars = mtcars
mymtcars$id = 1:nrow(mtcars)
runApp(
  list(ui = pageWithSidebar(
    headerPanel('Examples of DataTables'),
    sidebarPanel(
      checkboxGroupInput('show_vars', 'Columns to show:', names(mymtcars),
                         selected = names(mymtcars))
      ,textInput("collection_txt",label="Foo")
    ),
    mainPanel(
      DT::dataTableOutput("mytable")
    )
  )
  , server = function(input, output, session) {
    rowSelect <- reactive({
      paste(sort(unique(input[["rows"]])),sep=',')
    })
    observe({
      updateTextInput(session, "collection_txt", value = rowSelect() ,label = "Foo:" )
    })
    output$mytable = DT::renderDataTable({
      addCheckboxButtons <- paste0('<input type="checkbox" name="row', mymtcars$id, '" value="', mymtcars$id, '">',"")
      #Display table with checkbox buttons
      DT::datatable(cbind(Pick=addCheckboxButtons, mymtcars[, input$show_vars, drop=FALSE]),
                    options = list(orderClasses = TRUE,
lengthMenu = c(5, 25, 50),
pageLength = 25, 
callback = JS("function(table) {
    table.on('change.dt', 'tr td input:checkbox', function() {
          setTimeout(function () {
          Shiny.onInputChange('rows', $(this).add('tr td input:checkbox:checked').parent().siblings(':last-child').map(function() {
          return $(this).text();
          }).get())
          }, 10); 
          });
          }")),escape = FALSE,

                    )
    } 
    )
  }
  )
)

update

Make in other way using shinyinput

library(shiny)
library(DT)
mymtcars = mtcars
mymtcars$id = 1:nrow(mtcars)
runApp(
  list(ui = pageWithSidebar(
    headerPanel('Examples of DataTables'),
    sidebarPanel(
      checkboxGroupInput('show_vars', 'Columns to show:', names(mymtcars),
                         selected = names(mymtcars))
      ,textInput("collection_txt",label="Foo")
    ),
    mainPanel(
      DT::dataTableOutput("mytable")
    )
  )
  , server = function(input, output, session) {

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

    rowSelect <- reactive({

      rows=names(input)[grepl(pattern = "srows_",names(input))]
      paste(unlist(lapply(rows,function(i){
        if(input[[i]]==T){
          return(substr(i,gregexpr(pattern = "_",i)[[1]]+1,nchar(i)))
        }
      })))

    })

    observe({
      updateTextInput(session, "collection_txt", value = rowSelect() ,label = "Foo:" )
    })
    output$mytable = DT::renderDataTable({
      #Display table with checkbox buttons
    DT::datatable(cbind(Pick=shinyInput(checkboxInput,"srows_",nrow(mymtcars),value=NULL,width=1), mymtcars[, input$show_vars, drop=FALSE]),
                    options = list(orderClasses = TRUE,
                                   lengthMenu = c(5, 25, 50),
                                   pageLength = 25 ,

                                   drawCallback= JS(
                                     'function(settings) {
                                     Shiny.bindAll(this.api().table().node());}')
                                  ),selection='none',escape=F)


      } 
  )


    })
                    )
Batanichek
  • 7,761
  • 31
  • 49
  • thank you, :) what i must do when i want to see the true or false in checkbox? because when i try print(input$row1) the result is NULL, and when i give check in the checkbox there are no respon in print statement – lukman Jun 17 '16 at 16:02
  • may be need id instaed of name ( going to tets it tommorow) – Batanichek Jun 17 '16 at 16:29
  • But if all what you want -- select row and us eit as input in DT trere is row_selected input, see `selection` in `datatables` – Batanichek Jun 17 '16 at 19:22
  • sorry, i just new in shiny, my problem is how to get the value of print(input$row1),print(input$row2) etc, its like in my post question [link(]http://stackoverflow.com/questions/37886104/shiny-true-or-false-value-when-check-a-checkbox-in-datatable) – lukman Jun 18 '16 at 04:00
  • apologies for the necropost--I'm trying to use the shinyinput method for checkboxes, but the checked boxes are reset whenever the table refreshes. I submitted a question [here](https://stackoverflow.com/questions/76694807/how-do-i-make-persistent-checkboxes-with-dtdatatable-in-shiny) – saheed Jul 17 '23 at 13:37