Set names of values in lengthMenu (Page Length Menu) in R DT datatable

不羁岁月 提交于 2019-12-06 09:58:51

问题


I am making a datatable using the R DT package. I would like for the user to be able to decide/control whether to see 24, 48, 72, 96 or all rows in the data.

This can be easily done by setting:

lengthMenu = c(24,48, 72, 96, -1),

in the option list, where -1 stands for all entries.

Problem is that the user might not know what -1 stands for, and I would therefore like to make it appear as the string "All" in the menu visible to the user.

By looking at the documentation for lengthMenu, I see that this could be done by writing

$('#example').dataTable( {
"lengthMenu": [ [10, 25, 50, -1], [10, 25, 50, "All"] ]
} );

But I have no idea how to translate that to R-language. I have tried using named list, vectors and arrays but neither worked out.

Below is a simple example:

library(shiny)
library(DT)

ui <- fluidPage(
  DT::dataTableOutput("table")
)

server <- function(input, output) {
  output$table <- DT::renderDataTable({
    DT::datatable(iris, options = list(pageLength = 24, 
                  lengthMenu = c(24,48, 72, 96, -1), paging = T))
  })
}

shinyApp(ui, server) 

Any help will be greatly appreciated!

Thanks


回答1:


This should do. For more info please visit shiny-examples/018-datatable-options/

library(shiny)
library(DT)

ui <- fluidPage(
  DT::dataTableOutput("table")
)

server <- function(input, output) {
  output$table <- DT::renderDataTable({
    DT::datatable(iris, options = list(pageLength = 24,lengthMenu = list(c(24,48, 72, 96, -1), list('24', '48', '72','96', 'All')), paging = T))
  })
}

shinyApp(ui, server) 


来源:https://stackoverflow.com/questions/45509501/set-names-of-values-in-lengthmenu-page-length-menu-in-r-dt-datatable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!