R闪亮的数据表分页并显示所有行作为选项

时间:2019-03-13 23:46:58

标签: r shiny datatables

我在一个闪亮的应用程序中有一个数据表,我在其中进行分页以仅显示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
        )
      )
    )

    }
    )

3 个答案:

答案 0 :(得分:2)

如何使用按钮扩展名。我们定义了一个自定义按钮,该按钮调用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();
                                }")
            )
          )
        )
      )
    )

  }
)

答案 1 :(得分:1)

dom = "ft" 的选项中设置 renderDataTableHere 都是 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 动态化。

答案 2 :(得分:0)

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();}")

              )
              )
              )
            )
          )

  }
      )