[R] change default output size when using Sweave

Duncan Murdoch murdoch at stats.uwo.ca
Wed Apr 8 12:44:43 CEST 2009


Mark Heckmann wrote:
> Dear Duncan,
>
> Thanks for the reply. This works, but unfortunately I need a different
> solution.
> My script is supposed to run completely automated and the graphics I produce
> vary in size each time I run the script. But I want the graphics to be
> fitted to my .pdf output without specifying the height argument manually
> each time.
>   

If your documents are more or less fixed, you could add an extra 
preprocessor step before running Sweave.  For example, create mydoc.in 
containing

<<fig=TRUE, height=@SIZE@>>=
...
@

and then use sed or some other text substitution utility (even R, by 
reading your whole document into an R variable) to substitute the 
desired size for
the string @SIZE at .  It's clunky, but it sounds as though you're 
interested in producing standardized documents regularly, so it may be 
acceptable.

Duncan Murdoch
> That is why I do not want a fixed height as a code chunk argument. Actually
> I do not know if it is possible to have a variable placed in a code chunk
> header. I tried the following which does not work:
>
> <<>>=
> size <- 3
> @
>
> <<fig=TRUE, echo=TRUE, height=size>>=
>    pushViewport(viewport(height = unit(80, "mm")))    
>    grid.rect()
>    grid.text("I want this viewport to be the whole output size")
>    popViewport()
> @
>
> So still I face the problem to have Sweave generate a .pdf graphic that is
> just as big I want it to be.
>  
> In the Sweave Docu paragraph A.9 I discovered something I use as a
> workaround. I produce the .pdf output manually (where I can control the
> size) and add each graphic to LaTex manually as well.
>  
> <<results=tex, echo=FALSE>>=
> for (i in 1){
> file=paste("myfile", i, ".pdf", sep="")
> pdf(file=file, paper="special", width=6, height=3)
>    pushViewport(viewport(height = unit(5, "inches")))     
>    grid.rect()
>    grid.text("I want this viewport to be the whole output size")
>    popViewport()dev.off()
> cat("\\includegraphics{", file, "}\n\n", sep="")
> }
> @
>
> I use parenthesis around the code as it prints out something I do not want
> if no parenthesis are used (I use Windows). 
>
> I am not too happy with the solution. I would prefer a more straightforward
> approach to define the size of the output graphic. I wonder if there are
> some Sweave settings that can be modified. 
>
> In the Sweave manual (A.11) I found the following to customize the par
> settings for each figure:
>
> options(SweaveHooks=list(fig=function() par(bg="red", fg="blue")))
>
> I wonder if something similar could be done changing the size of the default
> output device (pdf or eps) for each figure like >pdf.options(height=2) or
> similar (it seems that this does not work)? 
>
> I suppose this type of graphic customization is quite a common issue when
> producing automated customized output/reports using R and Sweave but I
> haven't found anything concerning this topic yet.
>
> So I would be really glad if someone knows a solution.
>
> TIA, Mark
>
>
>
>
> -----Ursprüngliche Nachricht-----
> Von: Duncan Murdoch [mailto:murdoch at stats.uwo.ca] 
> Gesendet: Dienstag, 7. April 2009 19:08
> An: Mark Heckmann
> Cc: r-help at r-project.org
> Betreff: Re: [R] change default output size when using Sweave
>
> On 4/7/2009 10:39 AM, Mark Heckmann wrote:
>   
>> I am trying to create a graphic output in Sweave but I do not want it to
>>     
> be
>   
>> standard size. I want the whole graphic to be 80mm of height only, just
>>     
> like
>   
>> the viewport below.
>>
>> <<fig=TRUE, echo=TRUE>>=
>>    pushViewport(viewport(height = unit(80, "mm")))    
>>    grid.rect()
>>    grid.text("I want this viewport to be the whole output size")
>>    popViewport()
>> @
>>
>> How can I make the graphic smaller (80mm of height) without having the
>>     
> white
>   
>> margins at the top and bottom?
>>     
>
> You can specify height and width as arguments to the code chunk or set 
> new defaults with something like
>
> \SweaveOpts{width=5,height=3}
>
> (I think those measurements need to be given in inches; I don't think 
> there's a way to use an expression like unit(80,"mm").)
>
> You can also control the size of the plot in the final document with 
> LaTeX code like
>
> \usepackage{graphicx}
> \setkeys{Gin}{width=3in}
>
> and then the LaTeX rules for handling units would apply, so 80mm is fine 
> instead of 3in.
>
> Duncan Murdoch
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list