[R] Truncated R output in Sweave

Romain Francois romain.francois at dbmail.com
Mon Jun 8 09:50:40 CEST 2009


Frank E Harrell Jr wrote:
>
> Romain Francois wrote:
>>
>>
>> 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}
>
> Thank you Romain - I did not know of that construct.
>
>>
>>>
>>> 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() )
>
> Interesting.  What would be the corresponding batch file command to 
> achieve this?  I like to run R CMD Sweave my.Rnw.  
I have this as Hweave in my ${R_HOME}/bin directory:

#!/bin/env Rscript

args <- commandArgs( TRUE )
if( length( args ) < 1 ){
        stop( "need at least the file name" )
}
require( utils )
require( highlight, lib.loc = "~/.R/library" )
Sweave( args[1], driver = HighlightWeaveLatex() )

so that I can just do :

$ R CMD Hweave test.Rnw

> And would you mind giving a very brief comparison of the two 
> highlighting approaches regarding the LaTeX output?

SweaveListingUtils depends on listings and is basically based on a list 
of keywords that it recognizes.

highlight uses the R parser (or a slightly modified version of it) to 
identify the syntactic meaning of each token in the code. This gives you 
more control about how you want to code to be highlighted. It also uses 
true type font everywhere which I find more pleasing than what listings 
produces, it is a matter of taste I suppose.
highlight works with no modification on the sweave input file.


>
> Thanks!
> Frank
>
>> *||*
>>>
>>> Thanks
>>> Frank
>>
>>
>
>


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

-------------- next part --------------
A non-text attachment was scrubbed...
Name: test-listings.pdf
Type: application/pdf
Size: 18648 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090608/3f866d06/attachment-0004.pdf>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test-highlight.pdf
Type: application/pdf
Size: 13250 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20090608/3f866d06/attachment-0005.pdf>


More information about the R-help mailing list