Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

resize datatables to fit within R markdown

Tags:

r

r-markdown

dt

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.

like image 409
Rick Avatar asked Jun 27 '15 05:06

Rick


1 Answers

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

like image 96
Leni Ohnesorge Avatar answered Sep 24 '22 14:09

Leni Ohnesorge