R Shiny: How to add pagination in DT::renderDataTable - r

I am trying to add pagination, search box and selector in my R Shiny app, but it doesn't work for now (I tried paging = TRUE and searching = TRUE, in options as you can see bellow but it doesn't work). Do you have any idea of what I should add?
output$mytable1 <- DT::renderDataTable(
DT::datatable(
{ plots.dfs()[[1]] },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 2: ', htmltools::em('This is a simple caption for the table.')
),
extensions = 'Buttons',
options = list(
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'tB',
buttons = c('copy', 'csv', 'excel')
),
class = "display"
))
I have added a screenshot of table I have now, and the expected table.
Thanks for your help]1

You can modify the dom parameter, for example as follows:
DT::datatable(
{ mtcars },
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
'Table 2: ', htmltools::em('This is a simple caption for the table.')
),
extensions = 'Buttons',
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'Bftsp',
buttons = c('copy', 'csv', 'excel')
))
To add page length, also add l to the string. Hope this helps!

Related

R DataTables do not display Buttons above tabel

I have problem with the tabels in HTML file after rendering. They are displayed on the both sides of the tabel (in the way that is presented in the attached picture). What could be done to force the buttons to be displayed above the tabel and not on both sides of it?
I have also checked dom = 'frtBip' and dom = '<B>frtip' options and achived results like in the pictures.
datatable(data_tabel,
extensions = c("Buttons","KeyTable"), options = list(
dom = 'Bfrtip',
buttons = list('copy', 'print', list(
extend = 'collection',
buttons = list(
list(extend = c('csv'), filename = 'BPTrait'),
list(extend = c('excel'), filename = 'BPTrait'),
list(extend = c('pdf'), filename = 'BPTrait')
),
text = 'Download'
)),
keys = T
),
rownames=F,
colnames = c("Variant ID","Protein","Trait","-log10(p-value)"))
Do you want something like this ?
library(DT)
dtable <- datatable(iris,
extensions = c("Buttons","KeyTable"),
options = list(
dom = '<"top"B>frtip',
buttons = list('copy', 'print'),
keys = T
),
rownames=F)
library(htmltools)
htmlwidgets::prependContent(
dtable,
tags$style(HTML(".top {display:flex}"))
)
Or centered?
library(htmltools)
htmlwidgets::prependContent(
dtable,
tags$style(HTML(".top {display: flex; justify-content: center;}"))
)

Datatables Buttons to show/hide RowGroups

I have a table that uses RowGroups and Buttons/Colvis.
Much like the buttons for Colvis, I'd like to have buttons to show only specific RowGroups. Currently I have a long table with quite a few RowGroups. Often users will want to see only specific column groups and I'd like to prevent them from having to scroll down all the way constantly.
From the examples below, imagine there being a button "Row group" visibility, exactly like the Column visiblity in Colvis.
What I described can be accomplished using the SearchPanes extension. You can select your searchPanes targets on the same columns as your rowGroup targets.
You can also set your searchPanes to be a button.
Example from my code, with use in renderDT:
output$tbl <- renderDT({
datatable(data,
rownames = FALSE,
caption = 'Some caption',
extensions = c('ColReorder', 'RowGroup', "Buttons",
"Select", "SearchPanes"),
options = list(#DOM options
dom = "Bfrtip",
#Rowgroup options
rowGroup = list(dataSrc = c(0,2)),
#Buttons options
stateSave = TRUE,
buttons = c('colvis', 'csv', 'excel', 'searchPanes'),
#ColReorder options
colReorder = TRUE,
#SearchPanes options
columnDefs = list(
list(searchPanes = list(show = FALSE), targets = 1:7)),
selection = 'none',
#Overige options
paging = FALSE)
)}, server = FALSE)
Also credit to Matt Herman's blog post: https://mattherman.info/blog/dt_searchpanes/

R DataTables do not display Buttons and Length Menu simultaneously

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.

how to remove padding in datatable column

Is there any way of setting the datatable options to reduce the column padding? This link suggested using autoWidth=TRUE along with scrollX=TRUE, but it doesn't work in my code.
As you can see in the image below there is a big gap between columns forcing the user to scroll across, which I'd prefer to avoid if possible. This link and this one have the same problem in java
Here's the code for rendering the datatable.
output$book_table <- DT::renderDT(RVTables$book %>%
filter(deal==as.numeric(input$deal_choice)),
selection = list(mode="single",selected=row_edited),
editable = TRUE,
rownames = FALSE,
options=list(
autoWidth=TRUE,
scrollX = TRUE,
ordering=FALSE,
pageLength=12,
scrollY = TRUE,
bLengthChange= FALSE,
searching=FALSE
)
)
Thanks for any help.
After some google searching I found the line of code class="compact cell-border", which reduces the padding around column headers. Here is my code to render the table in case it helps anyone else.
output$book_table <- DT::renderDataTable({
DT::datatable(
deal_reactive(),
editable = TRUE,
rownames = FALSE,
class="compact cell-border",
selection = list(mode = "single",
target = "row",
selected = previous_row),
options = list(
dom="t",
autoWidth=TRUE,
scrollX = TRUE,
ordering=FALSE,
pageLength = 28,
bLengthChange= FALSE,
displayStart = previous_page,
searching=FALSE
)
)
})

Add download buttons in DT::renderDataTable

I am trying to add download buttons ('copy', 'csv', 'excel', 'pdf') above the table in my R Shiny app, but the renderDataTable seems doesn't work when using a datatable inside.
output$mytable1 <- DT::renderDataTable(
datatable(
{ plots.dfs()[[1]] },
rownames = TRUE,
options = list(
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = FALSE,
dom = 'tB',
buttons = c('copy', 'csv', 'excel', 'pdf')
),
class = "display"
))
When I use DT::renderDataTable without DT::datatable inside, renderDataTable works well and I have all features (filters, search field, etc), except download buttons (what I am trying to add)
output$mytable1 = DT::renderDataTable({ plots.dfs()[[1]] })
Do you have any idea of what I am doing wrong? Thanks for your help
As Stephan said in comment, the way to add buttons is the following:
output$mytable1 <- DT::renderDataTable(
DT::datatable(
{ plots.dfs()[[1]] },
extensions = 'Buttons',
options = list(
paging = TRUE,
searching = TRUE,
fixedColumns = TRUE,
autoWidth = TRUE,
ordering = TRUE,
dom = 'tB',
buttons = c('copy', 'csv', 'excel')
),
class = "display"
))
Adding one more solution to keep search and paging (dom = 'Bfrtip'):
datatable(data, extensions = "Buttons",
options = list(paging = TRUE,
scrollX=TRUE,
searching = TRUE,
ordering = TRUE,
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf'),
pageLength=5,
lengthMenu=c(3,5,10) ))
Adding an answer that is more explicit about allowing to download the whole table since it should be more clearly outlined in my opinion. Using renderDT({}) the download buttons only download the data currently being displayed. You can make the buttons download the entire dataset with renderDT(server = FALSE, {}) as used below:
renderDT(server=FALSE,{
# Load data
data <- mtcars
# Show data
datatable(data, extensions = 'Buttons',
options = list(scrollX=TRUE, lengthMenu = c(5,10,15),
paging = TRUE, searching = TRUE,
fixedColumns = TRUE, autoWidth = TRUE,
ordering = TRUE, dom = 'tB',
buttons = c('copy', 'csv', 'excel','pdf')))
})

Resources