[Tex/LaTex] Wrap text from xtable row names with knitr

knitrwrapxtable

I am using knitr to generate pdfs that contain tables that I'm making with xtable. How can I wrap the text in these tables?

Here is my best effort so far:

\documentclass{article}
\begin{document}

<<example>>=
Count = structure(c("0  (0%)", "1  (9%)", "1  (9%)", "1  (9%)", "2  (18%)", "0  (0%)", "0  (0%)", "0  (0%)", "3  (27%)", "7  (63%)", "4  (36%)", "6  (54%)", "6  (54%)", "3  (27%)", "4  (36%)", "8  (72%)", "6  (54%)", "2  (18%)", "6  (54%)", "2  (18%)", "2  (18%)", "1  (9%)", "3  (27%)", "3  (27%)", "2  (18%)", "1  (9%)", "0  (0%)", "2  (18%)", "1  (9%)", "5  (45%)", "3  (27%)", "0  (0%)", "0  (0%)", "0  (0%)", "0  (0%)", "0  (0%)", "0  (0%)", "2  (18%)", "1  (9%)", "0  (0%)"), .Dim = c(8L, 5L), .Dimnames = structure(list(variable = c("Gaining access to information from scientists", "Gaining access to information from public officials/government agencies", "Understanding the scientific research", "Synthesizing complex information for a general audience", "Explaining scientific uncertainty", "Successfully pitching stories to supervisors", "Struggling for placement or prominence of stories", "Obtaining professional development/research skills"), value = c("Very Difficult", "Somewhat Difficult", "Neutral", "Somewhat Easy", "Very Easy")), .Names = c("variable", "value")), class = "table")

row.names(Count) = stringr::str_wrap(row.names(Count), width=50)
print(xtable::xtable(Count, caption='test', label='tab:example', align=c(rep('l|', ncol(Count)), 'l')))
@

\end{document}

But the string::str_wrap() does not end up affecting the rows in the table. How can I wrap this text so that the table fits on a normal size page without having to resort to using the scalebox parameter of print()?

Best Answer

Try this:

<<results='asis',echo=FALSE>>=
# row.names(Count) = stringr::str_wrap(row.names(Count), width=50)

print(xtable(Count, caption='test', label='tab:example', align=c('p{1in}',rep('|c', ncol(Count)))),scalebox = 0.75)

@

The only change made is for the parameter align. The insertion of p{1in} tells that the first column should have the width of 1 inch. Rest is self explanatory.