[R] Truncated R output in Sweave

Romain Francois romain.francois at dbmail.com
Sat Jun 6 22:36:45 CEST 2009



Frank E Harrell Jr wrote:
>
> Dear Colleagues:
>
> In producing a book or a handout I sometimes cut out a large part of 
> the printed output of an R function, substituting ... for that chunk. 
> Deleting non-essential output saves paper.  It would be nice to have a 
> capability in Sweave to allow the user to specify a starting and an 
> ending regular expression that would identify consecutive lines of 
> output to replace with ..., without showing the extra code required to 
> do this to the user.  I would want the LaTeX output, other than the 
> omission, to appear the same as if the section were not omitted.  Does 
> anyone have an idea of how to do this elegantly?

Hello,

You could use the same trick that is sometimes used to embed lattice 
calls into print calls (see the grid vignette for example)

\documentclass[a4paper]{article}
\begin{document}
<<aa,eval=FALSE,echo=T>>=
rnorm( 200 )
<<echo=F>>=
txt <- capture.output( {
<<aa>>
    } )
if( length(txt) > 10 ){
    txt <- c( txt[1:10], "..." )
}
cat( txt, sep = "\n" )
@
\end{document}

>
> By the way I just discovered the SweaveListingUtils package by Peter 
> Ruckdeschel (on CRAN), which produces beautifully printed R code in 
> Sweave code chunks.  I put some info about it at 
> http://biostat.mc.vanderbilt.edu/SweaveTemplate which has our Sweave 
> template for statistical reports.
You might also be interested by the driver that comes with the highlight 
package (not yet on cran though, but the r-forge version is stable enough).

 > install.packages("highlight", repos="http://R-Forge.R-project.org")
 > require( highlight )
 > Sweave( file, driver = HighlightWeaveLatex() )
*||*
>
> Thanks
> Frank


-- 
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr




More information about the R-help mailing list