Skip to content

Sweave and abbreviating output from R

6 messages · Gavin Simpson, Gabor Grothendieck, Roger Bivand +1 more

#
Dear List,

I'm using Sweave to produce a series of class handouts for a course I am 
running. The students in previous years have commented about wanting 
output within the handouts so they can see what to expect the output to 
look like. So Sweave is a godsend for producing this type of handout - 
with one exception: Is there a way to suppress some rows of printed 
output so as to save space in the printed documentation? E.g

rnorm(100)

produces about 20 lines of output (depending on options("width")). I'd 
prefer something like:

rnorm(100)
   [1]  0.527739021  0.185551107 -1.239195562  0.020991608 -1.225632520
   [6] -1.000243373 -0.020180393  2.552180776 -1.719061533 -0.195024625
...
   [96] -0.744916379  0.863733400 -0.186667848  1.378236663 -0.499201046

The actual application would be printing of output from summary() 
methods. Ideally it would be nice to ask for line 1-10, 30-40, 100-102, 
for example, so you could print the first few lines of several sections 
of output. I'd like to automate this so I don't need to keep copying and 
pasting into the final tex source or forget to do it if I alter some 
previous part of the Sweave source.

Has anyone tried to do this? Does anyone know of an automatic way of 
achieving the simple abbreviation or the more complicated version I 
described?

Any thoughts on this?

Thanks in advance,

Gav
#
On Apr 11, 2005 7:22 AM, Gavin Simpson <gavin.simpson at ucl.ac.uk> wrote:
Maybe you could use head(rnorm(100)) instead.  Check ?head
for other arguments.
#
On Mon, 11 Apr 2005, Gavin Simpson wrote:

            
Semi-automatic is:
but I've found that 1) it needs masking from the users, and 2) it is 
difficult to automate when the numbers of output lines generated by print 
methods vary with input data (like in print.htest()). But capture.output 
is very useful.

Roger

  
    
#
Gabor Grothendieck wrote:
<snip>
Thanks for this Gabor. Head/tail works just fine for my rnorm example, 
but for printed output from summary methods for example it doesn't quite 
work as I would like.

But that got me thinking a bit more and I remembered capture.output() 
and I saw that noquote() is used in head.function(). So, I can now get 
this far:

noquote(capture.output(summary(pondsca)))[1:10]
  [1] 

  [2] Call: 

  [3] 

  [4] Partitioning of mean squared contingency coefficient: 

  [5] 

  [6] Total         4.996 

  [7] Unconstrained 4.996 

  [8] 

  [9] Eigenvalues, and their contribution to the mean squared 
contingency coefficient
[10] 


Now all I need is to find out how to print without the indices 
[1],[2]...[n] being printed?

Also, I noticed that with some objects, more than one "line" of output 
is printed per line - just like a vector would. In the example below we 
see 3 "lines" of output printed per row.

x <- rnorm(100)
y <- rnorm(100)
noquote(capture.output(summary(lm(x ~ y))))[1:5]
[1]                     Call:               lm(formula = x ~ y)
[4]                     Residuals:

Is there a way to print only a single element of a vector per line?

Thanks in advance.

Gav
#
On Apr 11, 2005, at 8:12 AM, Gavin Simpson wrote:

            
Any thought you would want to locally modify the print methods for 
object of interest?  I haven't tried it, but it might allow you to 
treat "large objects" like matrices, long vectors, and data.frames 
differently than objects like model.fits, etc.

Sean
#
Roger Bivand wrote:
<snip>
Thanks Roger - that got it! using combinations of:

<<echo=true,eval=FALSE>>=
summary(pondspca, scaling = 2)
@
<<echo=false,eval=true>>=
out <- capture.output(summary(pondspca))
cat(out[1:27], "....", out[43:48], "....", sep = "\n")
@

displays the relevant commands to the user but hides the semi-automatic 
printing of the selected sections.

All the best,

Gav