\documentclass[11pt]{article} \usepackage{verbatim} \usepackage{lmodern} \usepackage[utf8]{inputenc} \usepackage[margin=0.5in]{geometry} \usepackage{url} \setlength{\parindent}{0.0in} \setlength{\parskip}{0.1in} \raggedright \begin{document} \paragraph{The R environment for computing and graphics.} This follows the \texttt{Sweave} example at \url{http://jacobwegelin.net/prn/20120718Wed143201/}, with modifications so that we can use the \texttt{knitr} package instead of \texttt{Sweave}. Suppose you have a table (or matrix or dataframe) with a lot of text in a cell, such as the following. <>= library(knitr) library(xtable) library(tools) set.seed(5) bigmess<- data.frame( freeText=c("I have done the state some service and they know it. No more of that. I pray you, in your letters, When you shall these unlucky deeds relate, Speak of me as I am. Nothing extenuate, Nor set down aught in malice." , "ignotumque sibi mentula discat opus" , "denique cum membris conlatis flore fruuntur aetatis, iam cum praesagit gaudia corpus" , "cum dicis propero fac si facis Hedyle languet protinus et cessat debilitata Venus. exspectare jube. velocius ibo retentus.") , x=sample(1:199, size=4, replace=FALSE)) bigmess$group<- sample(LETTERS, replace=TRUE, size= nrow(bigmess)) # Note that bigmess has three variables (columns): names(bigmess) @ In knitr let's display the table. First we demonstrate that in knitr, the option ``results=`markup'{}'' is kinda like specifying the verbatim environment in \LaTeX{}. This is not what we want: <>= tmess<-xtable(as.matrix( bigmess)) print(tmess, include.rownames=FALSE) @ We will change to ``results=`asis'{}'' to get the table to display in \LaTeX{}. But when we ``naïvely'' use xtable with the default values for print(), the table marches off the page and we don't even see all of the first column: <>= tmess<-xtable(as.matrix( bigmess)) print(tmess, include.rownames=FALSE) @ \clearpage{} To remedy this, we'd like to squeeze or squish (depending on which part of the English-speaking world we're from) the ``freeText'' column into a ``multicolumn'' situation (in \LaTeX{} lingo), which is like opening a spreadsheet in Excel\textregistered{}, highlighting the first column, specifying ``wrap text'', and messing around with the mouse---but much better, because automated and reproducible. <>= align(tmess) <- c("r", "p{3.5in}", "c", "c") print(tmess, include.rownames=FALSE) @ But now let's run an identical chunk, except that ``echo=FALSE''. <>= align(tmess) <- c("r", "p{3.5in}", "c", "c") print(tmess, include.rownames=FALSE) @ The fine print: <>= # knit("knitfoo.Rnw") # # texi2dvi("knitfoo.tex", pdf = TRUE) sessionInfo() @ \end{document}