I need to set the column width of a DataTabe in an R Shiny app. I went through the Data Table Options in the documentation. Also, I reviewed this and this questions in Stackoverflow.
Setting width works fine if the DataTable does not have too many columns. In cases, however, if multiple columns present the settings is overridden despite the absolute width unit (e.g. '600px'
).
In the below code snippet:
output$mytable <- DT::renderDataTable({
num_of_cols <- 3
cbind(iris,iris)[,1:num_of_cols]},
options = list(autoWidth = TRUE,
columnDefs = list(list(width = '500px', targets = 1))))
if I set variable num_of_cols = 3
it works fine. Increasing displayed columns (num_of_cols
), however, leads to decrease in column width. In case of lots of displayed columns the width setting seemingly has no effect.
I tried option autoWidth = FALSE
but it gives no different result. I also tried using JavaScript in options drawCallback
as described in the answer section of this thread but it gives the same result.
How can I have DataTable display the desired column width setting?