3

I just noticed that datatable from DT library does not export all rows in the underlying dataset. It exports only visible rows. In the following reproducible example, it returns only 25 rows which are visible by default.

I wonder if there is any workaround to fix this.

library(shiny)
library(DT)

## Data table output format
data_output <- function(df) {
  DT::datatable(df, rownames= FALSE, options = list( dom = 'Bfrtip', buttons = c('excel','pdf','print','colvis'), pageLength = 25, initComplete = DT::JS(
    "function(settings, json) {",
    "$(this.api().table().header()).css({'background-color': '#369BE9', 'color': '#fff'});",
    "}") ), 
    extensions = c('Buttons','FixedColumns'))
}

## Shiny UI
ui <- basicPage(
  h2("The mtcars data"),
  DT::dataTableOutput("mytable")
)

## Shiny Server
server <- function(input, output) {
  output$mytable = DT::renderDataTable({
    data_output(iris)
  })
}

shinyApp(ui, server)
M.Qasim
  • 1,827
  • 4
  • 33
  • 58

1 Answers1

2
## Data table output format
  data_output <- function(df) {
       DT::datatable(df, rownames= FALSE, options = list( dom = 'Bfrtip', buttons = c('excel','pdf','print','colvis'), pageLength = nrow(df), initComplete = DT::JS(
"function(settings, json) {",
"$(this.api().table().header()).css({'background-color': '#369BE9', 'color': '#fff'});",
"}") ), 
extensions = c('Buttons','FixedColumns'))
}
A. Suliman
  • 12,923
  • 5
  • 24
  • 37
  • This will also show all rows.. How can I just show the 10 rows at a time, but when exported to csv/excel, I get the whole dataset? – SeGa Feb 28 '20 at 10:32
  • 1
    @SeGa what about DeanAttali's answer [here](https://stackoverflow.com/questions/50508854/button-extension-to-download-all-data-or-only-visible-data) – A. Suliman Feb 28 '20 at 16:13