簡體   English   中英

Shiny DT :: renderDataTable的“全選”復選框

[英]'Select All' checkbox for Shiny DT::renderDataTable

我想要一個復選框,用於選擇Shiny中標准DT :: renderDataTable中顯示的所有行(顯示的是鍵,因為這在您應用的過濾器與整個數據表之間有所不同)。

有沒有DT擴展已經這樣做了? 我的編碼技巧是基本的,所以我不能編寫等效的Java或HTML代碼。

這是我的應用程序到目前為止,任何csv文件都兼容所有目的。 目前有一種笨重的方式來創建所有選定行的另一個表(逐個手動選擇) - 當您想要選擇具有相同特征的30只動物時,這很困難。

library(shiny)
library(shinyjs)
library(DT)
library(dplyr)
library(data.table)



ui = pageWithSidebar(

  headerPanel(""),

  #This is where the full animal information file is input, as a ".txt" file.
  sidebarPanel(
    fileInput("ani", "Upload Animal Information File", accept = ".csv"),
    br(),
    numericInput("groups","Number of Ewe Groups", value = 1 ),
    #This is a list of the table headers. These headers can be indivdually selected to be part of the concatenated "Unique ID" single column.  
    uiOutput("choose_columns"),
    width = 2),
  mainPanel(
  DT::dataTableOutput("ani1"),
  DT::dataTableOutput("selectedEwes")
))






server = function(input, output, session) {

    animalinformation <- reactive({
        file1 <- input$ani
        if (is.null(file1))
            return(NULL)
        #This removes the Ewes and Status non-zero Rams from the displayed data, so that only live/at hand Rams are shown for selection.    
        isolate({
            anifile <- read.csv(file1$datapath, header = TRUE)
            anifile <- as.data.frame(anifile)
        })
        anifile
    })


    output$choose_columns <- renderUI({
        if (is.null(animalinformation()))
            return()
        colnames <- names(animalinformation())


        # Create the checkboxes and select them all by default
        checkboxGroupInput("columns", "Choose Columns",
                       choices = colnames,
                       selected = colnames)
    })




    #This line is repsonsible for creating the table for display.
    output$ani1 = DT::renderDataTable({
        if (is.null(animalinformation()))
            return()
        if (is.null(input$columns) || !(input$columns %in% names(animalinformation()))) { return() }
            { datatable(animalinformation()[, input$columns, drop = F], filter = "top") }
    })


    ani1_selected <- reactive({
        ids <- input$ani1_rows_selected
        animalinformation()[ids,]
    })





    #This displays the table of selected rows from the table of Rams. This table can be downloaded or printed, or copied using the buttons that appear above the table, thanks to the 'Buttons' extension.
    output$selectedEwes <- DT::renderDataTable({
        datatable(
      ani1_selected(),
      selection = list(mode = "none"),
      caption = "Copy to clipboard, download a .csv or print the following table of selected Ewes, using the above buttons.", extensions = 'Buttons', options = list(dom = 'Bfrtip', buttons = c('copy', 'csv', 'excel', 'pdf', 'print'))
    )
    })





}

shinyApp(ui = ui, server = server)

任何幫助將非常感謝謝謝。

這是我能想到的最簡單的實現。 它利用了DT將過濾器行索引返回到R的input$dt_rows_all ,在下面的示例中input$dt_rows_all 而且,它使用DT::dataTableProxy()來控制行選擇。 最后,它適用於客戶端模式和服務器端處理模式。

順便提一下,我想提一下,使用javascript來模仿DT中的選擇/取消選擇事件不會改變R中相關的閃亮綁定值(例如, input$dt_rows_selected )。 這是因為DT有自己的行選擇實現(可能在將來改變,但在編寫時尚未改變)。 如果您想了解更多信息,請參閱rstudio / DT#366

library(shiny)
ui <- tagList(
  DT::DTOutput("dt"),
  checkboxInput("dt_sel", "sel/desel all"),
  h4("selected_rows:"),
  verbatimTextOutput("selected_rows", TRUE)
)

server <- function(input, output, session) {
  dat <- reactive({iris})
  output$dt <- DT::renderDT(dat(), server = TRUE)
  dt_proxy <- DT::dataTableProxy("dt")
  observeEvent(input$dt_sel, {
    if (isTRUE(input$dt_sel)) {
      DT::selectRows(dt_proxy, input$dt_rows_all)
    } else {
      DT::selectRows(dt_proxy, NULL)
    }
  })
  output$selected_rows <- renderPrint(print(input$dt_rows_selected))
}

shiny::runApp(list(ui = ui, server = server))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM