Title: | Rich Text Format (RTF) Output |
---|---|
Description: | A set of R functions to output Rich Text Format (RTF) files with high resolution tables and graphics that may be edited with a standard word processor such as Microsoft Word. |
Authors: | Michael E. Schaffer [aut, cre, cph] |
Maintainer: | Michael E. Schaffer <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.4-14 |
Built: | 2025-02-16 05:01:35 UTC |
Source: | https://github.com/schaffman5/rtf |
A set of R functions to output Rich Text Format (RTF) files with high resolution tables and graphics that may be edited with a standard word processor such as Microsoft Word. This is useful for compiling R results into a document for further editing or to be merged with another document.
While Sweave and odfWeave provide nicely formatted outputs, the syntax can be difficult for beginners to master. Moreover, Sweave compilation to a PDF is not editable. Other packages such as SWord and R2wd provide similar functionality but require the user to be running a Microsoft Windows OS.
This package is written in pure R and does not require leaving the R environment to write files. R data frames and table objects are converted to nicely formatted RTF tables. One important limitation of the RTF specification is that vector graphics output is limited to Windows Meta File (WMF) and Enhanced Meta File (EMF) formats. Because these formats are not supported across platforms, this package currently only supports RTF embedding of PNG plots and images. To ensure high quality reports, the resolution may be specified when writing the RTF output.
This package depends on the R.oo package.
For usage details RTF
.
Michael E. Schaffer
[1] http://en.wikipedia.org/wiki/Rich_Text_Format
[2] http://latex2rtf.sourceforge.net/rtfspec_7.html#rtfspec_paraforprop
Insert a header into the RTF document.
## S3 method for class 'RTF' addHeader(this, title, subtitle=NULL, font.size=NULL, TOC.level=NULL, ...)
## S3 method for class 'RTF' addHeader(this, title, subtitle=NULL, font.size=NULL, TOC.level=NULL, ...)
this |
An RTF object. |
title |
Header title text. |
subtitle |
Header subtitle text. optional. |
font.size |
Font size in points. optional. |
TOC.level |
Indent level for table of contents. optional. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
Insert a new line into the RTF document.
## S3 method for class 'RTF' addNewLine(this, n=1, ...)
## S3 method for class 'RTF' addNewLine(this, n=1, ...)
this |
An RTF object. |
n |
Number of lines to add. Default is 1. optional |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
Insert a page break into the RTF document optionally specifying new page settings.
## S3 method for class 'RTF' addPageBreak(this, width=8.5, height=11, omi=c(1, 1, 1, 1), ...)
## S3 method for class 'RTF' addPageBreak(this, width=8.5, height=11, omi=c(1, 1, 1, 1), ...)
this |
An RTF object. |
width |
New page width in inches. optional. |
height |
New page height in inches. optional. |
omi |
A |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
rtf<-RTF("test_addPageBreak.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addPageBreak(rtf,width=11,height=8.5,omi=c(0.5,0.5,0.5,0.5)) done(rtf)
rtf<-RTF("test_addPageBreak.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addPageBreak(rtf,width=11,height=8.5,omi=c(0.5,0.5,0.5,0.5)) done(rtf)
Insert a paragraph into the RTF document.
## S3 method for class 'RTF' addParagraph(this, ...)
## S3 method for class 'RTF' addParagraph(this, ...)
this |
An RTF object. |
... |
A character |
Michael E. Schaffer
For more information see RTF
.
Insert a plot into the RTF document.
## S3 method for class 'RTF' addPlot(this, plot.fun=plot.fun, width=3, height=0.3, res=300, ...)
## S3 method for class 'RTF' addPlot(this, plot.fun=plot.fun, width=3, height=0.3, res=300, ...)
this |
An RTF object. |
plot.fun |
Plot function. |
width |
Plot output width in inches. |
height |
Plot output height in inches. |
res |
Output resolution in dots per inch. |
... |
Arguments for |
Plots are added to the document as PNG objects. This function will work with all base graphics methods for plotting. For more sophisticated plots, you may need to wrap your plot code into a function, and then pass a reference to that function to this method. The parameters for the plot method would then get passed in as '...' above.
To output a ggplot2 plot, simply assign the plot to a variable. Then use 'print' as the plot function and pass in the plot variable assigned above.
Michael E. Schaffer
For more information see RTF
.
rtf<-RTF("test_addPlot.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addPlot(rtf,plot.fun=plot,width=6,height=6,res=300, iris[,1],iris[,2]) done(rtf)
rtf<-RTF("test_addPlot.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addPlot(rtf,plot.fun=plot,width=6,height=6,res=300, iris[,1],iris[,2]) done(rtf)
Insert an existing PNG image into the RTF document.
## S3 method for class 'RTF' addPng(this, file, width=3, height=0.3, ...)
## S3 method for class 'RTF' addPng(this, file, width=3, height=0.3, ...)
this |
An RTF object. |
file |
Image file path. |
width |
Plot output width in inches. |
height |
Plot output height in inches. |
... |
Not used. |
Add existing PNG file to RTF document.
Michael E. Schaffer
For more information see RTF
.
Insert session information into the RTF document.
## S3 method for class 'RTF' addSessionInfo(this, locale=TRUE, ...)
## S3 method for class 'RTF' addSessionInfo(this, locale=TRUE, ...)
this |
An RTF object. |
locale |
Output the locale. |
... |
Not used. |
Exports session information to the RTF document in a similar
Michael E. Schaffer
For more information see RTF
., sessionInfo
.
rtf<-RTF("test_addSessionInfo.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addSessionInfo(rtf) done(rtf)
rtf<-RTF("test_addSessionInfo.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addSessionInfo(rtf) done(rtf)
Insert a table into the RTF document.
## S3 method for class 'RTF' addTable(this, dat, col.widths=NULL, col.justify=NULL, header.col.justify=NULL, font.size=NULL, row.names=FALSE, NA.string="-", space.before=NULL, space.after=NULL, ...)
## S3 method for class 'RTF' addTable(this, dat, col.widths=NULL, col.justify=NULL, header.col.justify=NULL, font.size=NULL, row.names=FALSE, NA.string="-", space.before=NULL, space.after=NULL, ...)
this |
An RTF object. |
dat |
A matrix, data frame, or table. |
col.widths |
A |
col.justify |
A single value or |
header.col.justify |
A single value or |
font.size |
Font size in points. optional. |
row.names |
Boolean argument to include row names in tables. optional. |
NA.string |
A character string to replace NA values in the table. |
space.before |
Space before each row (in inches). optional. |
space.after |
Space after each row (in inches). optional. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
rtf<-RTF("test_addTable.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addTable(rtf,as.data.frame(head(iris)),font.size=10,row.names=FALSE,NA.string="-", col.widths=rep(1,5)) tab<-table(iris$Species,floor(iris$Sepal.Length)) names(dimnames(tab))<-c("Species","Sepal Length") addTable(rtf,tab,font.size=10,row.names=TRUE,NA.string="-",col.widths=c(1,rep(0.5,4)) ) done(rtf)
rtf<-RTF("test_addTable.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addTable(rtf,as.data.frame(head(iris)),font.size=10,row.names=FALSE,NA.string="-", col.widths=rep(1,5)) tab<-table(iris$Species,floor(iris$Sepal.Length)) names(dimnames(tab))<-c("Species","Sepal Length") addTable(rtf,tab,font.size=10,row.names=TRUE,NA.string="-",col.widths=c(1,rep(0.5,4)) ) done(rtf)
Insert text into the RTF document.
## S3 method for class 'RTF' addText(this, ..., bold=FALSE, italic=FALSE)
## S3 method for class 'RTF' addText(this, ..., bold=FALSE, italic=FALSE)
this |
An RTF object. |
bold |
Bold text. optional. |
italic |
Italic text. optional. |
... |
Any number of character strings to concatenate. |
Michael E. Schaffer
For more information see RTF
.
Insert table of contents field.
## S3 method for class 'RTF' addTOC(this, ...)
## S3 method for class 'RTF' addTOC(this, ...)
this |
An RTF object. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
Insert a trellis plot object into the RTF document.
## S3 method for class 'RTF' addTrellisObject(this, trellis.object, width=3, height=0.3, res=300, rotate=NULL, ...)
## S3 method for class 'RTF' addTrellisObject(this, trellis.object, width=3, height=0.3, res=300, rotate=NULL, ...)
this |
An RTF object. |
trellis.object |
The trellis plot object. |
width |
Plot output width in inches. |
height |
Plot output height in inches. |
res |
Output resolution in dots per inch. |
rotate |
Object rotation in degrees. optional. |
... |
Not used. |
Plots are added to the document as PNG objects. Multi-page trellis objects are automatically split across multiple pages in the RTF output file. To rotate the object to landscape orientation within the RTF output, use rotate=90. When using rotation, width and height still refer to the unrotated plot dimensions and not the rotated output dimensions on the RTF page. An alternative to rotating the plot is to rotate the entire page using a call to addPageBreak with suitable page width and height dimensions.
Michael E. Schaffer
For more information see RTF
.
## Not run: rtf<-RTF("test_addTrellisObject.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) if(require(lattice) & require(grid)) { # multipage trellis object p2<-xyplot(uptake ~ conc | Plant, CO2, layout = c(2,2)) addTrellisObject(rtf,trellis.object=p2,width=8,height=4,res=300, rotate=90) } done(rtf) ## End(Not run)
## Not run: rtf<-RTF("test_addTrellisObject.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) if(require(lattice) & require(grid)) { # multipage trellis object p2<-xyplot(uptake ~ conc | Plant, CO2, layout = c(2,2)) addTrellisObject(rtf,trellis.object=p2,width=8,height=4,res=300, rotate=90) } done(rtf) ## End(Not run)
Decrease RTF document indent.
## S3 method for class 'RTF' decreaseIndent(this, ...)
## S3 method for class 'RTF' decreaseIndent(this, ...)
this |
An RTF object. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
Write and close the RTF output.
## S3 method for class 'RTF' done(this, ...)
## S3 method for class 'RTF' done(this, ...)
this |
An RTF object. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
End a paragraph in the RTF document.
## S3 method for class 'RTF' endParagraph(this, ...)
## S3 method for class 'RTF' endParagraph(this, ...)
this |
An RTF object. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
Increase RTF document indent.
## S3 method for class 'RTF' increaseIndent(this, ...)
## S3 method for class 'RTF' increaseIndent(this, ...)
this |
An RTF object. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
This is the class representing an RTF file output.
Package: rtf
Class RTF
Object
~~|
~~+--
RTF
Directly known subclasses:
public static class RTF
extends Object
RTF(file="", width=8.5, height=11, omi=c(1, 1, 1, 1), font.size=10)
RTF(file="", width=8.5, height=11, omi=c(1, 1, 1, 1), font.size=10)
file |
The path of the output file. |
width |
The width of the output page. |
height |
The width of the output page. |
omi |
A |
font.size |
Default font size for the document in points. |
... |
Not used. |
Methods:
addHeader |
Insert a header into the RTF document. | |
addNewLine |
Insert a new line into the RTF document. | |
addPageBreak |
Insert a page break into the RTF document optionally specifying new page settings. | |
addParagraph |
Insert a paragraph into the RTF document. | |
addPlot |
Insert a plot into the RTF document. | |
addPng |
Insert an existing PNG image into the RTF document. | |
addSessionInfo |
Insert session information into the RTF document. | |
addTOC |
Insert table of contents field. | |
addTable |
Insert a table into the RTF document. | |
addText |
Insert text into the RTF document. | |
addTrellisObject |
Insert a trellis plot object into the RTF document. | |
decreaseIndent |
Decrease RTF document indent. | |
done |
Write and close the RTF output. | |
endParagraph |
End a paragraph in the RTF document. | |
increaseIndent |
Increase RTF document indent. | |
setFontSize |
Set RTF document font size. | |
startParagraph |
Start a new paragraph in the RTF document. | |
view |
View encoded RTF. | |
Methods inherited from Object:
$, $<-, [[, [[<-, as.character, attach, attachLocally, clearCache, clearLookupCache, clone, detach, equals, extend, finalize, getEnvironment, getFieldModifier, getFieldModifiers, getFields, getInstantiationTime, getStaticInstance, hasField, hashCode, ll, load, names, objectSize, print, save
Michael E. Schaffer
For more information see RTF
.
## Not run: output<-"test_RTF-class.doc" png.res<-300 rtf<-RTF(output,width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addHeader(rtf,title="Test",subtitle="2011-08-15\n") addPlot(rtf,plot.fun=plot,width=6,height=6,res=300, iris[,1],iris[,2]) # Try trellis plots if(require(lattice) & require(grid)) { # single page trellis objects addPageBreak(rtf, width=11,height=8.5,omi=c(0.5,0.5,0.5,0.5)) p <- histogram( ~ height | voice.part, data = singer, xlab="Height") addTrellisObject(rtf,trellis.object=p,width=10,height=7.5,res=png.res) p <- densityplot( ~ height | voice.part, data = singer, xlab = "Height") addTrellisObject(rtf,trellis.object=p,width=10,height=7.5,res=png.res) # multipage trellis object p2<-xyplot(uptake ~ conc | Plant, CO2, layout = c(2,2)) addTrellisObject(rtf,trellis.object=p2,width=6,height=6,res=png.res) } addPageBreak(rtf, width=6,height=10,omi=c(0.5,0.5,0.5,0.5)) addTable(rtf,as.data.frame(head(iris)),font.size=10,row.names=FALSE,NA.string="-") addSessionInfo(rtf) done(rtf) ## End(Not run)
## Not run: output<-"test_RTF-class.doc" png.res<-300 rtf<-RTF(output,width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addHeader(rtf,title="Test",subtitle="2011-08-15\n") addPlot(rtf,plot.fun=plot,width=6,height=6,res=300, iris[,1],iris[,2]) # Try trellis plots if(require(lattice) & require(grid)) { # single page trellis objects addPageBreak(rtf, width=11,height=8.5,omi=c(0.5,0.5,0.5,0.5)) p <- histogram( ~ height | voice.part, data = singer, xlab="Height") addTrellisObject(rtf,trellis.object=p,width=10,height=7.5,res=png.res) p <- densityplot( ~ height | voice.part, data = singer, xlab = "Height") addTrellisObject(rtf,trellis.object=p,width=10,height=7.5,res=png.res) # multipage trellis object p2<-xyplot(uptake ~ conc | Plant, CO2, layout = c(2,2)) addTrellisObject(rtf,trellis.object=p2,width=6,height=6,res=png.res) } addPageBreak(rtf, width=6,height=10,omi=c(0.5,0.5,0.5,0.5)) addTable(rtf,as.data.frame(head(iris)),font.size=10,row.names=FALSE,NA.string="-") addSessionInfo(rtf) done(rtf) ## End(Not run)
Get an RTF encoded forest plot.
rtf.forest.plot(x=1.25, min=0.5, max=2, xlim=c(0.1, 12), width=3, height=0.3, cex=1, lwd=0.75, res=300)
rtf.forest.plot(x=1.25, min=0.5, max=2, xlim=c(0.1, 12), width=3, height=0.3, cex=1, lwd=0.75, res=300)
x |
x (e.g. hazard ratio). |
min |
Minimum whisker (e.g. lower bound of 95% hazard ratio CI). |
max |
Maximum whisker (e.g. upper bound of 95% hazard ratio CI). |
xlim |
A |
width |
Plot width in inches. |
height |
Plot height in inches. |
cex |
A numerical value giving the amount by which plotting text and symbols should be magnified relative to the default. |
lwd |
Line width. |
res |
Output resolution in dots per inch. |
Create a forest plot and convert PNG to RTF code. This is useful for embedding into a data frame of hazard ratios and then writing an RTF output file. See the example below for usage.
## Not run: tab<-data.frame( Label=c("Test1","Test2","Test3"), HR=c(1,2,0.45), Lower.CI=c(0.5,1.1,0.25), Upper.CI=c(2,3.5,0.9), stringsAsFactors=FALSE, check.names=FALSE) # create forest plots by row forest.plot.args<-list(xlim=c(0.1,5),width=3.0,height=0.3,cex=1,lwd=0.75,res=300) tab$"HR Plot (log scale)"<-mapply(rtf.forest.plot,tab$HR,tab$Lower.CI,tab$Upper.CI, MoreArgs=forest.plot.args) # rbind the x-scale to the table in the plot column xscale<-rtf.forest.plot.xscale(xlim=c(0.1,5),width=3.0,height=0.3,cex=1, lwd=0.75,res=300) tab<-data.frame(lapply(tab, as.character), stringsAsFactors=FALSE, check.names=FALSE) tab<-rbind(tab,list("","","","",xscale)) # write the RTF output rtf<-RTF("test_rtf.forest.plot.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addTable(rtf,tab,col.widths=c(0.75,0.75,0.75,0.75,3)) done(rtf) ## End(Not run)
## Not run: tab<-data.frame( Label=c("Test1","Test2","Test3"), HR=c(1,2,0.45), Lower.CI=c(0.5,1.1,0.25), Upper.CI=c(2,3.5,0.9), stringsAsFactors=FALSE, check.names=FALSE) # create forest plots by row forest.plot.args<-list(xlim=c(0.1,5),width=3.0,height=0.3,cex=1,lwd=0.75,res=300) tab$"HR Plot (log scale)"<-mapply(rtf.forest.plot,tab$HR,tab$Lower.CI,tab$Upper.CI, MoreArgs=forest.plot.args) # rbind the x-scale to the table in the plot column xscale<-rtf.forest.plot.xscale(xlim=c(0.1,5),width=3.0,height=0.3,cex=1, lwd=0.75,res=300) tab<-data.frame(lapply(tab, as.character), stringsAsFactors=FALSE, check.names=FALSE) tab<-rbind(tab,list("","","","",xscale)) # write the RTF output rtf<-RTF("test_rtf.forest.plot.doc",width=8.5,height=11,font.size=10,omi=c(1,1,1,1)) addTable(rtf,tab,col.widths=c(0.75,0.75,0.75,0.75,3)) done(rtf) ## End(Not run)
Set RTF document font size.
## S3 method for class 'RTF' setFontSize(this, font.size, ...)
## S3 method for class 'RTF' setFontSize(this, font.size, ...)
this |
An RTF object. |
font.size |
New default font size in points. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
Start a new paragraph in the RTF document.
## S3 method for class 'RTF' startParagraph(this, ...)
## S3 method for class 'RTF' startParagraph(this, ...)
this |
An RTF object. |
... |
Not used. |
Michael E. Schaffer
For more information see RTF
.
View encoded RTF.
## S3 method for class 'RTF' view(this, ...)
## S3 method for class 'RTF' view(this, ...)
this |
An RTF object. |
... |
Not used. |
Output the content of the object as RTF code.
Michael E. Schaffer
For more information see RTF
.