搜尋

首頁  >  問答  >  主體

在 R Shiny 中,如何製作一個複選框表,該表還具有單獨行/列的“全選”複選框?

使用 R Shiny,我想顯示一個表格(例如透過「DT」套件),其中每個單元格包含一個複選框。在每行和列標題旁邊,我想顯示一個“全選”/“主複選框”,選擇該複選框後,將選擇相應行或列中的所有複選框。作為附加功能,左上角儲存格中的核取方塊將選取表中的所有核取方塊。一個例子:

嘗試過的js

我在這裡找到了一個此功能的範例,其中一列有一個主複選框(使用一些 JavaScript),但無法弄清楚如何將其擴展到我的要求。

我嘗試過的範例

library(shiny)
library(DT)

ui <- fluidPage(
    # Sidebar panel
    sidebarPanel(),
    
    # Main panel with the table
    mainPanel(
        DTOutput("myTable")
    )
)

server <- function(input, output){
    dat <- data.frame(
        vapply(1:6, function(i){
            as.character(
                checkboxInput(paste0("col1-", i), label = NULL, width = "150px")
            )
        }, character(1)),
        vapply(1:6, function(i){
            as.character(
                checkboxInput(paste0("col2-", i), label = NULL, width = "150px")
            )
        }, character(1)),
        vapply(1:6, function(i){
            as.character(
                checkboxInput(paste0("col3-", i), label = NULL, width = "150px")
            )
        }, character(1))
    )
    
    names(dat) <- c(
        as.character(checkboxInput("col1", label = "1", width = "150px")),
        as.character(checkboxInput("col2", label = "2", width = "150px")),
        as.character(checkboxInput("col3", label = "3", width = "150px"))
    )
    
    row_names <- LETTERS[1:6]
    rownames(dat) <- row_names
    
    output$myTable <- renderDT({
        datatable(
            dat, 
            escape = FALSE,
            options = list(
                columnDefs = list(
                    list(targets = c(1, 2, 3), orderable = FALSE, className = "dt-center")
                )
            ),
            callback = JS(
                "$('#col1').on('click', function(){",
                "  var cboxes = $('[id^=col1-]');",
                "  var checked = $('#col1').is(':checked');",
                "  cboxes.each(function(i, cbox) {",
                "    $(cbox).prop('checked', checked);",
                "  });",
                "});",
                "$('#col2').on('click', function(){",
                "  var cboxes = $('[id^=col2-]');",
                "  var checked = $('#col2').is(':checked');",
                "  cboxes.each(function(i, cbox) {",
                "    $(cbox).prop('checked', checked);",
                "  });",
                "});",
                "$('#col3').on('click', function(){",
                "  var cboxes = $('[id^=col3-]');",
                "  var checked = $('#col3').is(':checked');",
                "  cboxes.each(function(i, cbox) {",
                "    $(cbox).prop('checked', checked);",
                "  });",
                "});"
            )
        )
    })
}

shinyApp(ui, server)

這是一個開始,但我無法弄清楚如何在行旁邊取得主複選框,也無法在所有框的左上角找到一個主複選框。另外,整個東西有點大 - 如果能更緊湊就更好了。

P粉884667022P粉884667022437 天前476

全部回覆(1)我來回復

  • P粉757640504

    P粉7576405042023-09-09 14:55:57

    library(shiny)
    library(DT)
    
    rowName <- function(L) {
      as.character(
        checkboxInput(paste0("row", L), label = L, width = "150px")
      )
    }
    rowNames <- vapply(LETTERS[1:6], rowName, character(1))
    
    
    ui <- fluidPage(
      # Sidebar panel
      sidebarPanel(),
      
      # Main panel with the table
      mainPanel(
        DTOutput("myTable")
      )
    )
    
    server <- function(input, output){
      dat <- data.frame(
        vapply(LETTERS[1:6], function(i){
          as.character(
            checkboxInput(paste0("col1-", i), label = NULL, width = "150px")
          )
        }, character(1)),
        vapply(LETTERS[1:6], function(i){
          as.character(
            checkboxInput(paste0("col2-", i), label = NULL, width = "150px")
          )
        }, character(1)),
        vapply(LETTERS[1:6], function(i){
          as.character(
            checkboxInput(paste0("col3-", i), label = NULL, width = "150px")
          )
        }, character(1))
      )
      
      names(dat) <- c(
        as.character(checkboxInput("col1", label = "1", width = "150px")),
        as.character(checkboxInput("col2", label = "2", width = "150px")),
        as.character(checkboxInput("col3", label = "3", width = "150px"))
      )
      
      rownames(dat) <- rowNames
      
      output$myTable <- renderDT({
        datatable(
          dat, 
          escape = FALSE,
          select = "none",
          options = list(
            columnDefs = list(
              list(targets = c(1, 2, 3), orderable = FALSE, className = "dt-center")
            ),
            initComplete = JS(
              "function() {",
              "  $('#col1').on('click', function(){",
              "    var cboxes = $('[id^=col1-]');",
              "    var checked = $('#col1').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#col2').on('click', function(){",
              "    var cboxes = $('[id^=col2-]');",
              "    var checked = $('#col2').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#col3').on('click', function(){",
              "    var cboxes = $('[id^=col3-]');",
              "    var checked = $('#col3').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowA').on('click', function(){",
              "    var cboxes = $('[id$=-A]');",
              "    var checked = $('#rowA').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowB').on('click', function(){",
              "    var cboxes = $('[id$=-B]');",
              "    var checked = $('#rowB').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowC').on('click', function(){",
              "    var cboxes = $('[id$=-C]');",
              "    var checked = $('#rowC').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowD').on('click', function(){",
              "    var cboxes = $('[id$=-D]');",
              "    var checked = $('#rowD').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowE').on('click', function(){",
              "    var cboxes = $('[id$=-E]');",
              "    var checked = $('#rowE').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowF').on('click', function(){",
              "    var cboxes = $('[id$=-F]');",
              "    var checked = $('#rowF').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "}"
            ),
            preDrawCallback = 
              JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
            drawCallback = 
              JS('function() { Shiny.bindAll(this.api().table().node()); } ')
          )
        )
      })
    }
    
    shinyApp(ui, server)
    

    編輯:「全選」複選框

    library(shiny)
    library(DT)
    library(htmltools)
    
    rowName <- function(L) {
      as.character(
        checkboxInput(paste0("row", L), label = L, width = "150px")
      )
    }
    rowNames <- vapply(LETTERS[1:6], rowName, character(1))
    
    
    sketch <- htmltools::withTags(
      table(
        class = "display",
        thead(
          tr(
            th(HTML(as.character(checkboxInput("allboxes", label = "ALL", width = "150px")))), 
            th(HTML(as.character(checkboxInput("col1", label = "1", width = "150px")))),
            th(HTML(as.character(checkboxInput("col2", label = "2", width = "150px")))),
            th(HTML(as.character(checkboxInput("col3", label = "3", width = "150px"))))
          )
        )
      )
    )
    
    
    
    ui <- fluidPage(
      br(),
      # Sidebar panel
      sidebarPanel(),
      
      # Main panel with the table
      mainPanel(
        DTOutput("myTable")
      )
    )
    
    server <- function(input, output){
      dat <- data.frame(
        vapply(LETTERS[1:6], function(i){
          as.character(
            checkboxInput(paste0("col1-", i), label = NULL, width = "150px")
          )
        }, character(1)),
        vapply(LETTERS[1:6], function(i){
          as.character(
            checkboxInput(paste0("col2-", i), label = NULL, width = "150px")
          )
        }, character(1)),
        vapply(LETTERS[1:6], function(i){
          as.character(
            checkboxInput(paste0("col3-", i), label = NULL, width = "150px")
          )
        }, character(1))
      )
      
      rownames(dat) <- rowNames
      
      output$myTable <- renderDT({
        datatable(
          dat, container = sketch,
          escape = FALSE,
          select = "none",
          options = list(
            columnDefs = list(
              list(targets = c(1, 2, 3), orderable = FALSE, className = "dt-center")
            ),
            initComplete = JS(
              "function() {",
              "  $('#allboxes').on('click', function(){",
              "    var cboxes = $('input[type=checkbox]');",
              "    var checked = $('#allboxes').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#col1').on('click', function(){",
              "    var cboxes = $('[id^=col1-]');",
              "    var checked = $('#col1').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#col2').on('click', function(){",
              "    var cboxes = $('[id^=col2-]');",
              "    var checked = $('#col2').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#col3').on('click', function(){",
              "    var cboxes = $('[id^=col3-]');",
              "    var checked = $('#col3').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowA').on('click', function(){",
              "    var cboxes = $('[id$=-A]');",
              "    var checked = $('#rowA').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowB').on('click', function(){",
              "    var cboxes = $('[id$=-B]');",
              "    var checked = $('#rowB').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowC').on('click', function(){",
              "    var cboxes = $('[id$=-C]');",
              "    var checked = $('#rowC').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowD').on('click', function(){",
              "    var cboxes = $('[id$=-D]');",
              "    var checked = $('#rowD').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowE').on('click', function(){",
              "    var cboxes = $('[id$=-E]');",
              "    var checked = $('#rowE').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "  $('#rowF').on('click', function(){",
              "    var cboxes = $('[id$=-F]');",
              "    var checked = $('#rowF').is(':checked');",
              "    cboxes.each(function(i, cbox) {",
              "      $(cbox).prop('checked', checked);",
              "    });",
              "  });",
              "}"
            ),
            preDrawCallback = 
              JS('function() { Shiny.unbindAll(this.api().table().node()); }'),
            drawCallback = 
              JS('function() { Shiny.bindAll(this.api().table().node()); } ')
          )
        )
      })
    }
    
    shinyApp(ui, server)
    

    回覆
    0
  • 取消回覆