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',
)
Related
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;}"))
)
I've wanted to remove the csv extension when downloading the file using the Data Table button download. Whenever i pressed the pdf button, it will named the download file as nameofthefile.csv.pdf . I do wanted to get only the name of the file only. Is there anyway on how i need to fix in my code to fix this?
#display the reactive stats datatable
output$statsDataframe <- renderDataTable({
statsDF_reactive$df()
}, extensions = 'Buttons', options = list(scrollX = TRUE,
dom = 'Bfrtip',
buttons = list(
list(extend = 'copy', title = input$file$name),
list(extend = 'csv', title = input$file$name),
list(extend = 'excel', title = input$file$name),
list(extend = 'pdf', title = input$file$name),
list(extend = 'print', title = input$file$name)
)))
statsDF_reactive$df() is my dataset after go through data transformation.
You can either use tools::file_path_sans_ext(input$file$name) or sub('\\.csv$', '', input$file$name) to remove the extension from filename.
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.
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)
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')
)
)