【问题标题】:R shiny datatable pagination and show all rows as optionsR闪亮的数据表分页并将所有行显示为选项
【发布时间】:2019-08-04 18:36:25
【问题描述】:

我在一个闪亮的应用程序中有一个数据表,我在其中进行分页以仅显示 15 行。但是我可以添加一个选项,用户可以使用分页一次看到 15 行,或者使用显示所有按钮来显示所有带有滚动条的记录。

library(shiny)
library(DT)
library(shinyWidgets)
library(shiny)


shinyApp(

  ui = navbarPage(
    title = 'DataTable',
    tabPanel('Display length',     DT::dataTableOutput('ex2'))
  ),

  server = function(input, output, session) {

    output$ex2 <- DT::renderDataTable(
      DT::datatable(
        iris, options = list(
          lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
          pageLength = 15
        )
      )
    )

    }
    )

【问题讨论】:

    标签: r shiny datatables


    【解决方案1】:

    怎么样,使用按钮扩展。我们定义了一个调用javascript函数page.len(-1)的自定义按钮,其中-1表示所有行:

    shinyApp(
    
      ui = navbarPage(
        title = 'DataTable',
        tabPanel('Display length',     DT::dataTableOutput('ex2'))
      ),
    
      server = function(input, output, session) {
    
        output$ex2 <- DT::renderDataTable(
          DT::datatable(
            iris, 
            extensions = 'Buttons',
            options = list(
              dom = 'Bfrtip',
              lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
              pageLength = 15,
              buttons = list(
                list(
                  extend = "collection",
                  text = 'Show All',
                  action = DT::JS("function ( e, dt, node, config ) {
                                        dt.page.len(-1);
                                        dt.ajax.reload();
                                    }")
                )
              )
            )
          )
        )
    
      }
    )
    

    【讨论】:

    • 谢谢。旁边是否有另一个按钮可以重置为 pagelength = 15 ?并在分页按钮附近显示全部按钮。
    • 知道了。谢谢。
    • 只是另一个小问题。展开后可以不滚动增加表格大小
    • 我并不真正关注该评论。你能更清楚地解释问题吗?如果它不是微不足道的,那么提出一个新问题可能比有后续评论更好。
    【解决方案2】:
    library(dplyr)
    library(shiny)
    library(DT)
    
    shinyApp(
    
      ui = navbarPage(
        title = 'DataTable',
        tabPanel('Display length',     DT::dataTableOutput('ex2'))
      ),
    
      server = function(input, output, session) {
    
        output$ex2 <- DT::renderDataTable(
          DT::datatable(
            iris, 
            extensions = 'Buttons',
            options = list(
              dom = 'tpB',
              lengthMenu = list(c(5, 15, -1), c('5', '15', 'All')),
              pageLength = 15,
              buttons = list(
                list(
                  extend = "collection",
                  text = 'Show All',
                  action = DT::JS("function ( e, dt, node, config ) {
                                  dt.page.len(-1);
                                  dt.ajax.reload();}")
                ),list(
                  extend = "collection",
                  text = 'Show Less',
                  action = DT::JS("function ( e, dt, node, config ) {
                                  dt.page.len(10);
                                  dt.ajax.reload();}")
    
                  )
                  )
                  )
                )
              )
    
      }
          )
    

    【讨论】:

      【解决方案3】:

      renderDataTable的选项中设置dom = "ft"Here 是所有 dom 选项。基本上这只是启用“f - 过滤”和“t - 表”。缺少“p 分页”。然后将pageLength 设置为非常大的显示(本例中为10000 行)*。以下是基于您的代码的最小示例:

      library(shiny)
      
      ui <- fluidPage(
        DT::dataTableOutput('my_table')
      )
      
      server <- function(input, output) {
        
        output$my_table <- DT::renderDataTable(
          iris, 
          options = list(dom = "ft",
                         pageLength = 10000)
        )
      }
      
      shinyApp(ui = ui, server = server)
      

      *更好的是,根据您的表格大小使pageLength 动态化。

      【讨论】:

        猜你喜欢
        • 2019-08-19
        • 2014-05-13
        • 2016-12-14
        • 2014-09-04
        • 2018-10-17
        • 2020-02-29
        • 1970-01-01
        • 1970-01-01
        • 2018-06-05
        相关资源
        最近更新 更多