簡體   English   中英

使用 Shiny ActionButton 到 select 所有行或將所有行添加到當前視圖中的選擇中,並在 DT 數據表中進行過濾

[英]Use Shiny ActionButton to select all rows or add all rows to selection in current view with filtering in a DT datatable

  1. 我一直在嘗試創建 ActionButtons 以允許用戶在反應式過濾數據表中“選擇視圖中的所有行”。

目前,該按鈕使用 tableid_rows_current 執行此操作; 但是,我還想添加一個表代理,這樣如果您在另一個頁面上,它就不會重置到結果的第一頁,但是經過多次谷歌搜索后我無法弄清楚語法(請參閱嘗試注釋掉代碼)。 此外,如果您手動 select 某些行,它不再起作用。

  1. 另一個允許用戶“將視圖中的所有行添加到選擇”的 ActionButton。 也就是說,將視圖中的所有當前行添加到您之前的選擇中。 這個我什至不知道從哪里開始,所以任何想法都值得贊賞。

(不包括在此處,但如果有人感興趣,我確實已經有功能“清除選擇”和“清除過濾器”按鈕)

下面的最小可重現代碼。 該應用程序旨在顯示所選行的圖像,但在這里您不會顯示實際圖像並不是什么大問題。

library(DT)
library(shiny)

dat <- data.frame(
  type = c("car", "truck", "scooter", "bike"),
  frontimage = c("carf.jpg", "truckf.jpg", "scooterf.jpg", "bikef")
)
  
# ----UI----
ui <- fluidPage(
  titlePanel("Buttons 'select all' and 'add to select'"),
  
  mainPanel(
    DTOutput("table"),
    actionButton("select_all_current", "Select All Rows in View"),
    actionButton("add_to_selection", "Add All Rows in View to Selection"),
    uiOutput("img1")
  )
)

# ----Server----
server = function(input, output, session){
  
  # Action button to select all rows in current view
  var <- reactiveValues()
  tableProxy <- dataTableProxy('table')
  
  observeEvent(input$select_all_current, {
    print("select_all_current")
    # tableProxy %>% selectRows(1:nrow(input$table_rows_current))
    # var$selected <- tableProxy %>% input$table_rows_current
    
    tableProxy <- #I want the table proxy to be whatever the current selection and filters are and the current page view to stay the same after selecting
      var$selected <- input$table_rows_current
  })
  
  # Action button to add all rows in current view to previous selection
  observeEvent(input$add_to_selection, {
    print("select_all_current")
    
  })
  
  # Data table with filtering
  output$table = DT::renderDT({
    datatable(dat, filter = list(position = "top", clear = FALSE), 
              selection = list(target = 'row', selected = var$selected),
              options = list(
                autowidth = TRUE,
                pageLength = 2,
                lengthMenu = c(2, 4)
              ))
  })
  
  # Reactive call that only renders images for selected rows 
  df <- reactive({
    dat[input[["table_rows_selected"]], ]
  })
  
  # Front image output
  output$img1 = renderUI({
    imgfr <- lapply(df()$frontimage, function(file){
      tags$div(
        tags$img(src=file, width="100%", height="100%")
      )
      
    })
    do.call(tagList, imgfr)
  })

}
# ----APP----    
# Run the application 
shinyApp(ui, server)

這是否符合您的要求?

library(DT)
library(shiny)

dat <- data.frame(
  type = c("car", "truck", "scooter", "bike"),
  frontimage = c("carf.jpg", "truckf.jpg", "scooterf.jpg", "bikef")
)

# ----UI----
ui <- fluidPage(
  titlePanel("Buttons 'select all' and 'add to select'"),
  
  mainPanel(
    DTOutput("table"),
    actionButton("select_all_current", "Select All Rows in View"),
    actionButton("add_to_selection", "Add All Rows in View to Selection"),
    uiOutput("img1")
  )
)

# ----Server----
server = function(input, output, session){
  
  # Action button to select all rows in current view
  var <- reactiveValues()
  tableProxy <- dataTableProxy('table')
  
  observeEvent(input$select_all_current, {
    print("select_all_current")
    # tableProxy %>% selectRows(1:nrow(input$table_rows_current))
    # var$selected <- tableProxy %>% input$table_rows_current
    
    # tableProxy <- #I want the table proxy to be whatever the current selection and filters are and the current page view to stay the same after selecting
      # var$selected <- input$table_rows_current
    selectRows(proxy = tableProxy,
               selected = input$table_rows_current)
  })
  
  # Action button to add all rows in current view to previous selection
  observeEvent(input$add_to_selection, {
    print("select_all_current")
    
    selectRows(proxy = tableProxy,
               selected = c(input$table_rows_selected, input$table_rows_current))
    
  })
  
  # Data table with filtering
  output$table = DT::renderDT({
    datatable(dat, filter = list(position = "top", clear = FALSE), 
              selection = list(target = 'row'),#, selected = var$selected),
              options = list(
                autowidth = TRUE,
                pageLength = 2,
                lengthMenu = c(2, 4)
              ))
  })
  
  # Reactive call that only renders images for selected rows 
  df <- reactive({
    dat[input[["table_rows_selected"]], ]
  })
  
  # Front image output
  output$img1 = renderUI({
    imgfr <- lapply(df()$frontimage, function(file){
      tags$div(
        tags$img(src=file, width="100%", height="100%")
      )
      
    })
    do.call(tagList, imgfr)
  })
  
}
# ----APP----    
# Run the application 
shinyApp(ui, server)

暫無
暫無

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

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