简体   繁体   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. I have been trying to create ActionButtons to allow a user to 'Select all rows in view' in a reactive, filtering datatable.我一直在尝试创建 ActionButtons 以允许用户在反应式过滤数据表中“选择视图中的所有行”。

Currently the button does this using tableid_rows_current;目前,该按钮使用 tableid_rows_current 执行此操作; however, I also want to add in a table proxy so that it doesn't reset to the first page of results if you're on another page, but I can't figure out the syntax after much googling (see attempts commented out in code).但是,我还想添加一个表代理,这样如果您在另一个页面上,它就不会重置到结果的第一页,但是经过多次谷歌搜索后我无法弄清楚语法(请参阅尝试注释掉代码)。 Also if you manually select some rows, it no longer works.此外,如果您手动 select 某些行,它不再起作用。

  1. Another ActionButton that allows a user to 'add all rows in view to selection'.另一个允许用户“将视图中的所有行添加到选择”的 ActionButton。 That is to add all current rows in view to your previous selection.也就是说,将视图中的所有当前行添加到您之前的选择中。 This one I'm not even sure where to start, so any ideas are appreciated.这个我什至不知道从哪里开始,所以任何想法都值得赞赏。

(Not included here, but I do have functioning 'clear selection' and 'clear filter' buttons already, if anyone is interested) (不包括在此处,但如果有人感兴趣,我确实已经有功能“清除选择”和“清除过滤器”按钮)

Minimum reproducible code below.下面的最小可重现代码。 The app is meant to display the images for the selected rows, but not a big deal here that you won't have actual images displaying.该应用程序旨在显示所选行的图像,但在这里您不会显示实际图像并不是什么大问题。

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)

Does this do what you're looking for?这是否符合您的要求?

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