I have got two datatables in my shiny application that are located in separate tabs. One of them has buttons for column selection and for exporting to xlsx, the other one only for column selection. After starting the app, if I go to the tab with datatable that has just one button first and than to the second one, the second one does not have export to xlsx button anymore. However if I go to the datatable with two buttons first and then to the other one then both datatables have buttons that they were supposed to have. Do you have any ideas on how to solve this?
library(shiny)
library(shinydashboard)
library(DT)
library(dplyr)
# Define UI for app that draws a histogram ----
ui <- dashboardPage(
dashboardHeader(title = "Basic dashboard"),
dashboardSidebar(
sidebarMenu(
menuItem('intro', tabName = 'intro'),
menuItem('df1', tabName = 'df1'),
menuItem('df2', tabName = 'df2')
)),
dashboardBody(
tabItems(
tabItem('intro', 'Hello'),
tabItem(tabName = 'df1',
sliderInput('horsepower', label = 'horsepower', min = min(mtcars$hp), max = max(mtcars$hp), value = c(min(mtcars$hp), max(mtcars$hp))),
dataTableOutput('dt1t')
),
tabItem(tabName = 'df2',
dataTableOutput('dt2t')
)
)
)
)
server <- function(input, output) {
output$dt1t <- renderDataTable({
mtcars %>%
filter(hp >= input$horsepower[[1]], hp <= input$horsepower[[2]]) %>%
datatable(extensions = c('Scroller', 'Buttons'), options = list(
dom = 'Bt',
buttons = list('colvis', list(extend = 'excel', text = 'Export to xlsx')),
scroller = T,
scrollY = 500,
scrollX = T),
escape = F,
selection = 'single',
rownames = F)
})
output$dt2t <- renderDataTable({
iris %>%
datatable(extensions = c('Scroller', 'Buttons'), options = list(
dom = 'Bt',
buttons = list('colvis'),
scroller = T,
scrollY = 500,
scrollX = T),
escape = F,
selection = 'single',
rownames = F)
})
}
shinyApp(ui = ui, server = server)
This bug seems to have been squashed in 923545
, an update of the DataTables library and some of the extensions. I'm guessing it was a bug in the Buttons extension (upgraded from 1.2.0 to 1.4.1), but not exactly sure.
So update DT to version 0.2.15 or later and the issue should be gone. You can get 0.2.15 by running devtools::install_github("rstudio/DT@923545")
, or install the latest from CRAN, or install the latest dev version on GitHub.
note: to reproduce the issue, you can install DT 0.2.14 by running devtools::install_github("rstudio/DT@b2c0c9")