3

I asked a question a while back about how to wrap long table cell content in an RMarkdown document. A nice answer pointed to the pander() package.

I'm facing a similar issue again, only I'm working in an Rnw file, and my understanding is that pander() does not work with LaTeX. So I'm back to trying to figure out how to wrap long lines in kable().

\documentclass{article}
\begin{document}

This is my test

<<test, echo=FALSE>>=
library(knitr)
test <- data.frame(v1=c("This is a long string. This is a long string. This is a long string. This is a long string. This is a long string.",
                        "This is a another long string. This is a another long string. This is a another long string. This is a another long string. This is a another long string."),
                   v2=c(1, 2))
kable(test)
@

\end{document}
Community
  • 1
  • 1
Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • Hi, @eric-green it looks like this questions really bother you a lot. lol I saw your [question about it earlier this year](http://stackoverflow.com/questions/29425499/wrap-long-text-in-kable-table-column) Why don't you submit a question on `knitr`'s github page? – Hao Oct 30 '15 at 19:18
  • It does not keep me up at night, but as noted in my question I did come across it before in a different form. I thought about submitting an issue, but I'm not sure that this is a problem that kable needs to solve. As the answer I accepted points out, xtable might fill this void. – Eric Green Oct 30 '15 at 19:23
  • I actually run into a similar case today and I found your posts. `xtable` is definitely not my favorite package and I would also love to see `kable` has this functionality in the future. :) – Hao Oct 30 '15 at 19:26
  • I've come around to xtable, but I also like the simplicity of kable. I guess it is hard to want lots of customization and simplicity at the same time – Eric Green Oct 30 '15 at 19:32
  • The above linked question has an answer using `kable()`. – iled Apr 12 '19 at 03:54

1 Answers1

3

If you are compiling the pdf with latex I recommend package xtable you can replicate almost everything you have in the tabular environment. The align option gives you what you want: l - left, c - center, r - right, or the size of the column. I added other options for you to play around. In the chunk options you have to add results = 'asis'.

library(xtable)
print(xtable(test, caption="A caption", align="lp{2cm}p{2cm}"), 
      comment=F, include.rownames=F, table.placement="ht", 
      size=getOption("xtable.size", "tiny"))
animalito
  • 382
  • 2
  • 7
  • that's probably a good recommendation if there are no workarounds for `kable()`. i'll likely recommend this answer unless a `kable()` solution is posted (since this was my initial question). thanks! – Eric Green Oct 21 '15 at 20:08