Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R DataTables do not display Buttons and Length Menu simultaneously

Tags:

r

dt

shiny

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.

enter image description here

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.

enter image description here

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.

enter image description here

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)
like image 677
BRCN Avatar asked Dec 03 '22 19:12

BRCN


2 Answers

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.

like image 55
SuKu Avatar answered Dec 11 '22 15:12

SuKu


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.

like image 28
Vishesh Shrivastav Avatar answered Dec 11 '22 16:12

Vishesh Shrivastav