SarahGC
SarahGC

Reputation: 507

R: Change width of entire datatable in Rmd

I'm generating a report using R markdown that includes a datatable. I would like the resulting html to maintain some margins, rather than the table filling the whole width of the page.

I've tried including:

knitr::opts_chunk$set(fig.width=5, fig.height=8) 

I've tried this:

 ``` {r fig1, fig.height = 3, fig.width = 5, echo = FALSE, include=TRUE}

I've tried defining figure size in the header under output. I've played around with the column widths option in the data table itself.

Any suggestions?

Here's an example Rmd:

---
title: "Initial Data Check Report"
date: "`r format(Sys.time(), '%B %d, %Y %H:%M:%S', 'America/Chicago', T)`"
output:
  html_document:
    self_contained: yes
    theme: null
    highlight: null
    mathjax: null
params:
  client: "ClientName"

---

### Audit Table
Some text here

---

```{r table, echo = FALSE, include=FALSE}
library(dplyr)
library(DT)
data = mtcars

```

``` {r fig1, fig.height = 3, fig.width = 5, echo = FALSE, include=TRUE}
datatable(data)

```

Upvotes: 5

Views: 4505

Answers (1)

Weihuang Wong
Weihuang Wong

Reputation: 13118

Would something like

<div style = "width:80%; height:auto; margin: auto;">
``` {r fig1, echo = FALSE, include=TRUE}
datatable(data)
```
</div>

work?

Upvotes: 9

Related Questions