When you click the DataTable 1 tab, you can see the Length Menu at the top which is the main point of this question. The bit starting with output$ex1
in the server codes up this data table.
Actually I would like to have buttons for both column visibility and downloading the data. A little modification to the output$ex1
by adding buttons
argument
is required. Then, you end up with output$ex2
. But now I miss the Length Menu.
I thought adding pageLength = 5, lengthMenu = c(5, 10, 15, 20)
to the output$ex2
as can be seen in the code starting with output$ex3
would solve this issue and I can have the Length Menu back. But third one returned me exactly the same table as the second one.
1) How can I have both the buttons and the Length Menu?
2) How can I arrange the positions of the buttons? For example, I would like the buttons for the colvis to be above the buttons for downloading.
3) When you look at the code below, you see that excel is also included in the buttons: buttons = c('colvis', 'copy', 'csv', 'excel', 'pdf', 'print')
But in none of the last 2 tables can you see the button for excel.
Additionally, I do not receive an error for that.
I would appreciate any help.
library(shiny)
ui <- navbarPage(
title = 'DataTable Options',
tabPanel('DataTable 1', DT::dataTableOutput('ex1')),
tabPanel('DataTable with Buttons 1', DT::dataTableOutput('ex2')),
tabPanel('DataTable with Buttons 2', DT::dataTableOutput('ex3'))
)
server <- function(input, output) {
output$ex1 <- DT::renderDataTable(
DT::datatable(iris,
class = 'cell-border stripe',
filter = 'top',
options = list(autoWidth = TRUE)))
output$ex2 <- DT::renderDataTable(
DT::datatable(iris,
class = 'cell-border stripe',
filter = 'top', extensions = 'Buttons',
options = list(autoWidth = TRUE,
dom = 'Bfrtip',
buttons = c('colvis', 'copy', 'csv', 'excel',
'pdf', 'print'))))
output$ex3 <- DT::renderDataTable(
DT::datatable(iris,
class = 'cell-border stripe',
filter = 'top', extensions = 'Buttons',
options = list(autoWidth = TRUE,
pageLength = 5, lengthMenu = c(5, 10, 15, 20),
dom = 'Bfrtip',
buttons = c('colvis', 'copy', 'csv', 'excel',
'pdf', 'print'))))
}
shinyApp(ui = ui, server = server)
You need to add "l" (small letter "L") to dom, that makes Blfrtip:
B - Buttons
l - Length changing input control
f - Filtering input
r - pRocessing display element
t - Table
i - Table information summary
p - Pagination control
These features can be positioned with following command:
"dom": "<'row'<'col-md-3'B><'col-md-6'l><'col-md-3'f>><'row'<'col-md-12't>><'row'<'col-md-3'i><'col-md-6'><'col-md-3'p>>"
Just like we do it in Bootstrap.
For showing both buttons and length menu, you have to change your dom
to 'Blfrtip'
.
output$ex2 <- DT::renderDataTable(
DT::datatable(iris,
class = 'cell-border stripe',
filter = 'top', extensions = 'Buttons',
options = list(autoWidth = TRUE,
dom = 'Blfrtip',
buttons = c('colvis', 'copy', 'csv', 'excel', 'pdf', 'print'))))
If you open your app in Chrome, you can see the excel
button. This is one of the issues of datatable according to this link.
For positioning the buttons, you'll most likely have to make changes in their HTML tags; something along the lines of this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With