Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R Shiny Dashboard DataTable Column Width [duplicate]

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.

like image 569
MMAASS Avatar asked Nov 25 '25 02:11

MMAASS


1 Answers

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

like image 136
Stéphane Laurent Avatar answered Nov 27 '25 17:11

Stéphane Laurent



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!