How to export DT table to html and preserve all functionality?

Viewed 44

I'm able to export a DT table generated in R/RStudio to HTML using the htmlWidget:saveWidget method. However, the FixedColumns feature is not preserved and becomes very narrow when a term is entered in the search bar.

xyz_search_dt <- datatable(
  xyz_search_table_d,
  rownames = FALSE, extensions = 'Buttons', 
  options = list(autoWidth = TRUE, 
                 extensions = 'FixedColumns', 
                 options = list(dom = 't',scrollX = TRUE, 
                                fixedColumns = TRUE),
                 columnDefs = list(list(width= '200px',
                                        targets = "feedback")),
                 dom=('Bfrtip'), buttons = c('excel'),
                 pageLength = table_rows,
                 searchHighlight = TRUE),
  filter = list(position="top"))

htmlwidgets::saveWidget(xyz_search_dt, "xyz_search_dt.html")
2 Answers

You can try this. I use the mtcars dataset and everything works well.

xyz_search_dt <- DT::datatable((mtcars),
                           rownames = FALSE, 
                           extensions = 'Buttons', 
                           options = list(autoWidth = TRUE, 
                                          extensions = 'FixedColumns', 
                                          options = list(dom = 't', 
                                                         scrollX = TRUE, 
                                                         fixedColumns = TRUE),
                                          columnDefs = list(list(width= '200px',
                                                                 targets = "feedback")), 
                                          dom=('Bfrtip'), 
                                          buttons = c('excel'),
                                          #pageLength = table_rows,
                                          searchHighlight = TRUE),
                           filter=list(position="top"))

htmlwidgets::saveWidget(xyz_search_dt, "xyz_search_dt.html")

OUTPUT: enter image description here

Related