-->

R Shiny Dashboard DataTable Column Width [duplicat

2019-07-28 11:12发布

问题:

This question already has an answer here:

  • R Shiny set DataTable column width 2 answers

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.

回答1:

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 )")
    ))
  )
)