I am building a Shiny dashboard displaying a DataTable.
My objective is to have the column width of data table automatically adjusted to fit the cell values in 1 line instead of multiple lines. However, I got the following result:
It seems like Name was adjusted as what I want, but Notes did not. Then, I followed those links: https://rstudio.github.io/DT/options.html,
Setting column width in R Shiny DataTable does not work in case of lots of column,
R Shiny set DataTable column width
Basically, the solutions suggested online were to use autoWidth = TRUE
argument with columnDefs
when rendering data table.
This did not work for me as well, below is the result:
It seems like the width of the table was narrowed comparing with the initial result
Below is my final code:
header <- dashboardHeader(
title = "Test"
)
sidebar <- dashboardSidebar(
)
body <- dashboardBody(
box(title = "Test", width = 7, status = "warning", DT::dataTableOutput("df"))
)
# UI
ui <- dashboardPage(header, sidebar, body)
# Server
server <- function(input, output, session) {
output$df = DT::renderDataTable(df, options = list(
autoWidth = TRUE,
columnDefs = list(list(width = '10px', targets = c(1,3)))))
}
# Shiny dashboard
shiny::shinyApp(ui, server)
I am not sure why the width of Name column can be adjusted with the length of cell value, but that of Notes cannot. I also tried using data table in other projects, the width of the columns can only be adjusted with the length of column name.
Another option in my case would be setting the column width to be only a certain number of characters and having the full context of cell values showing up by hovering over the cell. The solution was suggested here: R Shiny Dashboard DataTable Column Width. However, this is for DT only, but it did not work out when I integrate it in Shiny.
Thanks in advance.