Datatables Buttons to show/hide RowGroups - r

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/

Related

Make datatable #DT rownames not clickable

I have a table made with the DT package where my cells are clickable. However, I would like to have the cells in the row names not clickable or in other word, specify a range of columns, something like ‘selected = [2:5]’ where cells are actually active for a click base behavior.
Hope the problem is quiet enough visual. Thanks for your help.
You mean the click for the selection ? The code below works for me but I'm not using the latest version of DT.
output$dtable <- renderDT({
datatable(iris, extensions = "Select", selection = "none",
options = list(
columnDefs = list(
list(className = "not-selectable", targets = 0)
),
select = list(
style = "single",
selector = "td:not(.not-selectable)"
)
)
)
})

R large datatable display in Shiny

I am trying to display a relatively large DataTable in a shiny app. The table's dimensions are 7000x30. The performance in Chrome was very very sluggish so I added the 'Scroller' extension to limit the rendering but that did not help. It seems that it is the large number of columns that is causing the issue, not the large number of rows. I also tried to used the colvis extension but the button gives a non scrollable list and with 30 columns, that wouldn't work. Also I tried to have some columns hidden using the visible option, but that didn't work.
Here is the example:
data = as_tibble(matrix(runif(200000), ncol=30))
data %>%
DT::datatable(filter = 'top', extensions = c('Buttons', 'Scroller'),
options = list(scrollY = 650,
scrollX = 500,
deferRender = TRUE,
scroller = TRUE,
# paging = TRUE,
# pageLength = 25,
buttons = list('excel',
list(extend = 'colvis', targets = 0, visible = FALSE)),
dom = 'lBfrtip',
fixedColumns = TRUE),
rownames = FALSE)
Weirdly, the Rstudio viewer shows the datatable and is fine. It's only when I Run the document as a shiny document and open it in Chrome that it becomes very sluggish. My questions are:
Why is this happening
How do I only show a limited number of columns by default and have the option to show the others
is there a better button for colvis? if the list of columns exceeds the page length, I can't access those hidden columns to toggle them on or off.
What do you mean by sluggish?
I ran it and everything looks okay to me in terms of speed.
library(shiny)
library(shinydashboard)
library(DT)
####/UI/####
header <- dashboardHeader()
sidebar <- dashboardSidebar()
body <- dashboardBody(
DT::dataTableOutput("test")
)
ui <- dashboardPage(header, sidebar, body)
####/SERVER/####
server <- function(input, output, session) {
data <- as_tibble(matrix(runif(200000), ncol=30))
output$test <- DT::renderDataTable({
DT::datatable(
data,
filter = 'top', extensions = c('Buttons', 'Scroller'),
options = list(scrollY = 650,
scrollX = 500,
deferRender = TRUE,
scroller = TRUE,
# paging = TRUE,
# pageLength = 25,
buttons = list('excel',
list(extend = 'colvis', targets = 0, visible = FALSE)),
dom = 'lBfrtip',
fixedColumns = TRUE),
rownames = FALSE)
})
}
shinyApp(ui, server)
You may want to remove 'scroller' from Options = () as with that many rows, you may want to break it up into pages. Also, you can try to make the table server processing by putting Server = TRUE in there, that will make it process every page versus the whole dataset at once.

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.

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')))
})

Change color of tabletools buttons in DT library in R

Instead of grey change to a custom color
For example this:
library(DT)
iris2 = head(iris, 20)
# only show the Copy and Print buttons
datatable(
iris2,
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('copy', 'print')
)
)
Gives us the following:
What i would like is to change the color of the buttons Copy and Print.
I have gone through https://datatables.net/extensions/buttons/examples/
but i am not able to find a solution.
You can include some javascript/jquery to change the colors of the buttons in the callback:
datatable(
iris2,
callback=JS('$("button.buttons-copy").css("background","red");
$("button.buttons-print").css("background","green");
return table;'),
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('copy', 'print')
)
)

Resources