1

When I output my R Markdown (knitr / RStudio) to html the following table stretches the full width of the browser you view it in. It's only two columns and looks rather odd stretched out on a widescreen display.

Col1 | Col2
--- | ---
1 | 1
1349 | 143910

This same table shown below, same syntax, correctly limits the width of Column 1 to the width of its own contents. The only difference is the cell contents of position [2, 2] are extremely long.

Col1 | Col2
--- | ---
1 | 1
1349 | 143910143910143910143910143910143910143910143910143910

How do I force knitr or pandoc or R or whatever to limit column width to only slightly larger than the columns contents. Why did the extreme number of characters in cell [2, 2] force my output to behave as I wish? I didn't involve any CSS in the second table and prefer not to mess around with CSS.

stackinator
  • 5,429
  • 8
  • 43
  • 84

1 Answers1

1

I suggest you use kable from the knitr package and kable_styling from the kableExtra package.

Supposing your dataframe is df

kable(df, "html") %>%
  kable_styling(full_width = F)

You can find more info here.

https://www.rdocumentation.org/packages/kableExtra/versions/0.6.1/topics/kable_styling

EDIT: eipi10´S clarifications on packages. (Thanks!)

Neoromanzer
  • 434
  • 2
  • 15