Gaurav Singhal
Gaurav Singhal

Reputation: 1086

r markdown: data frame (or data.table) goes out of page

I am trying to create an HTML output using R - markdown. The problem is that whenever I try to output any table, the formatting is very sparse. Ideally the table should be easily fitted in one page width but since the formatting is too sparse, one needs to scroll to the right to see the output.

Here is the code and output.

```{r}
df = data.frame(
  first.var = 1:10, 
  second.var = letters[1:10],
  third.var = LETTERS[1:10],
  fourth.var = paste0(letters[1:10],"-",LETTERS[1:10]),
  fifth.var = "this will not go out of screen",
  sixth.var = "but this will go out of the screen"
)
df
```

enter image description here

How can I fit more columns in one page width, so that I don't have to scroll.

Upvotes: 0

Views: 1110

Answers (1)

Ryan Morton
Ryan Morton

Reputation: 2695

Try setting the style (CSS) for the table. I wrapped the table into DT::datatable:

<div style="width = 100%">
```{r}
df = data.frame(
  first.var = 1:10, 
  second.var = letters[1:10],
  third.var = LETTERS[1:10],
  fourth.var = paste0(letters[1:10],"-",LETTERS[1:10]),
  fifth.var = "this will not go out of screen",
  sixth.var = "but this will go out of the screen"
)
DT::datatable(df)
```
</div>

Upvotes: 1

Related Questions