Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

dt

shiny

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

like image 471
JesperHansen Avatar asked Oct 21 '25 14:10

JesperHansen


1 Answers

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), 
                                          c('24', '48', '72','96', 'All')),
                         paging = T))
  })
}

shinyApp(ui, server) 
like image 66
Pork Chop Avatar answered Oct 23 '25 03:10

Pork Chop



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!