1

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")
Kat
  • 15,669
  • 3
  • 18
  • 51
tjf64
  • 11
  • 1
  • Welcome to Stack Overflow. You can [format](https://stackoverflow.com/help/formatting) the code in your question to make it easier to read. Don't forget to also take the [tour] and read [ask] for further guidance. – andrewJames Jul 15 '22 at 18:44

2 Answers2

0

The solution that I found and seems to work well is to address the widget sizing policy after the fact.

dow_search_dt[["sizingPolicy"]][["defaultWidth"]] <- "100%"
htmlwidgets::saveWidget(dow_search_dt, "dow_search_dt.html")

Reference:

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
tjf64
  • 11
  • 1
-1

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

Isaac
  • 382
  • 2
  • 7
  • This answer simply reuses my original code with pagination. I think the problem is with the saveWidget function because the text-oriented search table maintains the fixed-width column when rendered through RStudio's viewer. However, when saved using saveWidget function and opened as a self-contained HTML file, the fixed-width column functionality does not seem to come along. – tjf64 Jul 16 '22 at 18:28