R DataTables do not display Buttons above tabel - r

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;}"))
)

Related

How to download only the selected columns in a dataframe using Colvis from DT in Shiny?

I am using the button colvis from the DT package to select which columns I would like to show in the table. Here you have more info about the button colvis.
It works perfectly fine, it hides the columns that I don't want to select and the result is shown to the user.
However, it seems that this info is not updated when I download the file.
If I only select "Petal.Width" and "Species":
Then, I download the file... and I open it. I still have all the columns and not the selected ones.
I have been trying to find a solution, but I haven't found anything.
Does anyone know how to fix it?
Thanks in advance.
Here is my code:
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput("table")
)
server <- function(input, output, session) {
output$table <- renderDataTable({
datatable(
iris,
filter = list(position = 'top', clear = FALSE),
selection = "none", #this is to avoid select rows if you click on the rows
rownames = FALSE,
extensions = 'Buttons',
options = list(
scrollX = TRUE,
dom = 'Blrtip',
buttons =
list(I('colvis'),'copy', 'print', list(
extend = 'collection',
buttons = list(
list(extend = 'csv', filename = paste0("iris"), title = NULL),
list(extend = 'excel', filename = paste0("iris"), title = NULL)),
text = 'Download'
)),
lengthMenu = list(c(10, 30, 50, -1),
c('10', '30', '50', 'All'))
),
class = "display"
)
})
}
shinyApp(ui, server)
library(DT)
datatable(
iris,
extensions = "Buttons",
options = list(
dom = "Bfrtip",
buttons = list(
I("colvis"),
list(
extend = "collection",
text = "Download",
buttons = list(
list(
extend = "csv",
exportOptions = list(
columns = ":visible"
)
)
)
)
)
)
)
Thanks to Stéphane Laurent's answer, I managed to find an answer.
I had some problems to have both buttons (csv and excel) and how to organise the lists with the proposed solution, but I found the way to do it.
I will add the answer with the original code just in case someone has problems like me.
library(shiny)
library(DT)
ui <- fluidPage(
dataTableOutput("table")
)
server <- function(input, output, session) {
output$table <- renderDataTable({
datatable(
iris,
filter = list(position = 'top', clear = FALSE),
selection = "none", #this is to avoid select rows if you click on the rows
rownames = FALSE,
extensions = 'Buttons',
options = list(
scrollX = TRUE,
dom = 'Blrtip',
buttons =
list(I('colvis'),'copy', 'print', list(
extend = 'collection',
text = 'Download',
buttons = list(
list(
extend = "csv", filename = paste0("iris"), title=NULL,
exportOptions = list(
columns = ":visible")
),
list(
extend = "excel", filename = paste0("iris"), title=NULL,
exportOptions = list(
columns = ":visible")
)
)
)),
lengthMenu = list(c(10, 30, 50, -1),
c('10', '30', '50', 'All'))
),
class = "display"
)
})
}
shinyApp(ui, server)

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/

Change filename when downloading data from datatable R

I'm using datatable inside an R shiny web app.
How can I change the file name that is gonna be created when donwloading a datatable object?
For example:
datatable(
iris2,
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons =
list('copy', 'print', list(
extend = 'collection',
buttons = c('csv', 'excel', 'pdf'),
text = 'Download'
))
)
)
I want the file donwloaded to be named by default "iris.xlsx" or "iris.csv" . Thanks
Because of extend = "collection", you need to include the filename argument through a nested list inside the button = list(...)
library(DT)
datatable(
iris,
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons =
list('copy', 'print', list(
extend = 'collection',
buttons = list(
list(extend = 'csv', filename = "iris"),
list(extend = 'excel', filename = "iris"),
list(extend = 'pdf', filename = "iris")),
text = 'Download'
))
)
)
Without a collection of buttons
If you don't want a collection of sub-buttons and instead wanted to specify a filename for a single file type (let's say a CSV file), you can do this
library(DT)
datatable(
iris,
extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons =
list('copy', 'print', list(
extend = 'csv',
title = "interesting_file"))))
THe key is to use extend = "csv" to specify the file type and then use title for the filename, all inside a list.

Button extension to download all data or only visible data

With the button extension to DT package, is there a way to specify that the buttons download either (1) all the data feeding the datatable, or (2) only the data on the visible page.
Below is the example from the documentation.
datatable(
iris, extensions = 'Buttons', options = list(
dom = 'Bfrtip',
buttons = c('copy', 'csv', 'excel', 'pdf', 'print')
)
)
As #divibisan said, one option is to use the server argument of renderDT() to control whether the download button will download only the current or all rows.
That would work well if you want to have one download button. However if you want to have two buttons always appear, where one download the current page and one downloads the full dataset, you can use the following code:
library(shiny)
ui <- fluidPage(
DT::DTOutput("table")
)
server <- function(input, output, session) {
output$table <- DT::renderDT(server = FALSE, {
DT::datatable(
mtcars,
extensions = c("Buttons"),
options = list(
dom = 'Bfrtip',
buttons = list(
list(extend = "csv", text = "Download Current Page", filename = "page",
exportOptions = list(
modifier = list(page = "current")
)
),
list(extend = "csv", text = "Download Full Results", filename = "data",
exportOptions = list(
modifier = list(page = "all")
)
)
)
)
)
})
}
shinyApp(ui, server)
See this answer: Buttons: download button with scroller downloads only few rows
Whether the buttons export all data or only visible data is determined by the server argument in the DT::renderDT function call. If server=FALSE then the buttons will export all data in the table, while if server=TRUE they will only export visible data.
You could set the server argument with a variable to make this a selectable option.
output$table <- DT::renderDT(server = input$download_all, {
DT::datatable( ... )
}
The other option you might want to look at is the exportOptions: modifier: selected option that determines whether to download only selected rows (the default) or all rows. You can read about that option here: https://datatables.net/extensions/buttons/examples/print/select.html
Note that your users might run into performance and memory issues using server=FALSE if your data table is very large.
you are looking for the modifiers: page: selected. here is a working example
ui <- fluidPage(
title = "Examples of DataTables",
sidebarLayout(
mainPanel(
tabsetPanel(
id = 'dataset',
tabPanel("diamonds", DT::dataTableOutput("mytable1"))
)
)
)
)
server <- function(input, output) {
# choose columns to display
diamonds2 = diamonds[sample(nrow(diamonds), 1000), ]
output$mytable1 <- DT::renderDataTable({
DT::datatable(diamonds2,
extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons =
list(
list(
extend = 'csv',
buttons = c('csv'),
exportOptions = list(
modifiers = list(page = "current")
)
))
)
)
})
}
shinyApp(ui, server)
hope this helps!
If you want to include the options to download the current page and the entire dataset as both a csv or an excel file, I've managed to implement this as two separate dropdown buttons with those options, together with the copy and print buttons:
Here is the modified working code:
library(shiny)
ui <- fluidPage(
DT::dataTableOutput("table")
)
server <- function(input, output, session) {
output$table <- DT::renderDataTable(server = FALSE, {
DT::datatable(
mtcars,
extensions = "Buttons",
filter = "top",
selection = "none", #this is to avoid select rows if you click on the rows
rownames = FALSE,
options = list(
scrollX = TRUE,
autoWidth = FALSE,
dom = 'Blrtip', # the important thing is that there is the l to allow for the lengthMenu
# https://stackoverflow.com/questions/52645959/r-datatables-do-not-display-buttons-and-length-menu-simultaneously
buttons = list(
# insert buttons with copy and print
# colvis includes the button to select and view only certain columns in the output table
# from https://rstudio.github.io/DT/extensions.html
I('colvis'), 'copy', 'print',
# code for the first dropdown download button
# this will download only the current page only (depends on the number of rows selected in the lengthMenu)
# using modifier = list(page = "current")
# only the columns visible will be downloaded using the columns:":visible" option from:
# https://stackoverflow.com/questions/72317260/how-to-download-only-the-selected-columns-in-a-dataframe-using-colvis-from-dt-in/72317607#72317607
list(
extend = 'collection',
buttons = list(
list(extend = "csv", filename = "page",exportOptions = list(
columns = ":visible",modifier = list(page = "current"))
),
list(extend = 'excel', filename = "page", title = NULL,
exportOptions = list(columns = ":visible",modifier = list(page = "current")))),
text = 'Download current page'),
# code for the second dropdown download button
# this will download the entire dataset using modifier = list(page = "all")
list(
extend = 'collection',
buttons = list(
list(extend = "csv", filename = "data",exportOptions = list(
columns = ":visible",modifier = list(page = "all"))
),
list(extend = 'excel', filename = "data", title = NULL,
exportOptions = list(columns = ":visible",modifier = list(page = "all")))),
text = 'Download all data')
),
# add the option to display more rows as a length menu
lengthMenu = list(c(10, 30, 50, -1),
c('10', '30', '50', 'All'))
),
class = "display"
)
})
}
shinyApp(ui, server)

custom name dowloaded csv from the datatable package R

I have a problem in changing the name of csv downloaded, using the extension buttons of the datatable package, I want custom name of de csv, I'm trying to use the option JS for writing JavaScript code, but I'm not successful
library(DT)
datatable(head(iris, 30),
extensions = 'Buttons',
options = list(
dom = 'Bfrtip',
buttons = list(list(extend = 'collection', buttons = c('csv','pdf'),text = 'Descarga'))
),
callback = JS("
{
extend: csv,
text: Save Log Info,
filename: download1}
"))
The configuration shld be in the options directly:
DT::datatable(
mtcars,
options = list(
dom = 'Bfrtip',
buttons = list(
list(
extend = 'csv',
text = "Save Log Info",
title = 'download1'
),
'pdf'
)
),
extensions = 'Buttons',
)

Resources