[R] fit printed output onto a single page

Jim Lemon jim at bitwrit.com.au
Wed May 5 11:05:46 CEST 2010


On 05/05/2010 12:12 AM, Abiel X Reinhart wrote:
> Is there a way to force a certain block of captured output to fit onto a single printed page, where one can specify the properties of the page (dimensions, margins, etc)? For example, I might want to generate 10 different cuts of a data table and then capture all the output into a PDF, ensuring that each run of the data table fits onto a single page (i.e. the font-size of the output may have to be dynamically adjusted).
>
Hi Abiel,
One way that comes to mind is to display the block of text on a graphics 
device like postscript. First open the postscript device and create an 
empty plot:

# notice how culturally sensitive I am, using "letter" size paper!
postscript("mytext.ps",width=8.5,height=11)
# set the margins to zero or something narrow
par(mar=c(0,0,0,0))
plot(0,xlab="",ylab="",xlim=c(0,1),ylim=c(0,1),type="n",axes=FALSE)

Then work out whether your block of text will fit into the plot:

blockwidth<-strwidth(mytextblock)
blockheight<-strheight(mytextblock)
# this should get the reduction (or expansion) to make it fit
cexmult<-1/max(c(blockwidth,blockheight)

text(0.5,0.5,mytextblock,cex=cexmult)

This is off the top of my head (which is a bit dented at the moment) so 
you might want to try it out first.

Jim



More information about the R-help mailing list