resize datatables to fit within R markdown

10.9k views Asked by At

I am trying to embed datatable in the R Markdown ioslides. However for a table with more than 8 col, it would not be able to fit within one page. Is there in the r chuck to re-size table to fit within ioslide? I have tried to use width in the options, but seems to have no effect.

## Example table, use another data wider tan iris
```{r, message=FALSE, warning=FALSE, echo=FALSE}
library(DT)
datatable(iris, options=list(pageLength = 5))
```

Above code is for reference only, not to reproduce the issue. In some use cases, the datatable is wider than screen and hope to be able to re-size to fit within one slide. Thanks.

1

There are 1 answers

0
Leni Ohnesorge On BEST ANSWER

DT package suggests to fixed certain columns and add scroll. See Section 4 here

m = as.data.frame(round(matrix(rnorm(100), 5), 5))
datatable(
  m, extensions = 'FixedColumns',
  options = list(
  dom = 't',
  scrollX = TRUE,
  scrollCollapse = TRUE
))

You can also look at the default css that datatable uses and try to adjust the width there. Maybe this is helpfull: https://datatables.net/examples/basic_init/flexible_width.html