3

I am building a Shiny dashboard and one panel on the dashboard is a DataTable.

Below is my code:

  output$table = DT::renderDataTable(b1, selection = 'single')

The width of columns in the data table is now adjusted with the width of column name. However, some cell values are text, and those text are squeezed to display in multiple lines as they are longer than the column names.

I am wondering if there is a way to have the column width adjusted to fit the cell values in one line.

Or, is there a way to set a fixed width for the columns and get the full content of cell value by hover over?

Thanks in advance.

Stéphane Laurent
  • 48,421
  • 14
  • 86
  • 170
MMAASS
  • 353
  • 3
  • 13
  • Thank you, I checked this and used columnDefs in Option argument provided in the answer provided in this post. However, it did not worked out for me. – MMAASS Mar 14 '19 at 15:23
  • Then you need to show a [mcve] of your problem which shows how the answers from the duplicate fail to work. If you do that, it will let us diagnose what is different about your problem from the one in the duplicate question. Without that, though, answering your question further is impossible. – divibisan Mar 14 '19 at 17:38

1 Answers1

5

You can use the ellipsis plugin to limit the number of visible characters of the cells and to have the full content of the cells in a tooltip.

library(DT) 

dat <- data.frame(
  A = c("fnufnufroufrcnoonfrncacfnouafc", "fanunfrpn frnpncfrurnucfrnupfenc"),
  B = c("DZDOPCDNAL DKODKPODPOKKPODZKPO", "AZERTYUIOPQSDFGHJKLMWXCVBN")
)

datatable(
  dat, 
  plugins = "ellipsis",
  options = list(
    # limit cells in columns 1 and 2 to 17 characters
    columnDefs = list(list(
      targets = c(1,2),
      render = JS("$.fn.dataTable.render.ellipsis( 17, false )")
    ))
  )
)

enter image description here

Stéphane Laurent
  • 48,421
  • 14
  • 86
  • 170