
Insert Content into Microsoft Word
Source:R/toWrd.R, R/toWrd_TMod.R, R/toWrd_TOne.R, and 3 more
toWrd.RdInserts content into an active Microsoft Word document via RDCOMClient.
The function supports different input types and dispatches methods based
on the class of x.
Usage
toWrd(x, font = NULL, ..., wrd = NULL)
# S3 method for class 'summaryDefault'
toWrd(x, font = NULL, ..., wrd = NULL)
# Default S3 method
toWrd(x, font = NULL, newpara = TRUE, ..., wrd = NULL)
# S3 method for class 'character'
toWrd(
x,
font = NULL,
para = NULL,
style = NULL,
bullet = FALSE,
newpara = FALSE,
...,
wrd = NULL
)
# S3 method for class 'TMod'
toWrd(
x,
font = list(name = "Arial Narrow", size = 10),
digits = 3,
naForm = "-",
verbose = NULL,
rowtitle = "",
labels = NULL,
...,
wrd = NULL
)
# S3 method for class 'TOne'
toWrd(
x,
font = list(name = "Arial Narrow", size = 10),
align = NULL,
indent = strrep(" ", 3),
legend = TRUE,
...,
wrd = NULL
)
# S3 method for class 'data.frame'
toWrd(
x,
font = list(name = "Arial Narrow", size = 10),
align = NULL,
halign = "c",
decpos = NULL,
spacing = c(before = 1, after = 0, line = 1.25),
symbols = TRUE,
na = "",
digits = NULL,
fmt = NULL,
autofit = "window",
newpara = TRUE,
...,
wrd = NULL
)
# S3 method for class 'matrix'
toWrd(
x,
font = list(name = "Arial Narrow", size = 10),
rownames = !is.null(base::rownames(x)),
rowtitle = NULL,
labels = NULL,
digits = NULL,
fmt = NULL,
align = NULL,
...,
wrd = NULL
)
# S3 method for class 'table'
toWrd(
x,
font = list(name = "Arial Narrow", size = 10),
freqtitle = "Freq",
...,
wrd = NULL
)
# S3 method for class 'WrdPlot'
toWrd(
x,
font = NULL,
width = 15,
height = NULL,
asp = 2/3,
type = c("png", "emf", "svg"),
res = 300,
pointsize = 10,
align = "c",
newpara = TRUE,
...,
wrd = NULL
)
# S3 method for class 'recordedplot'
toWrd(
x,
font = NULL,
width = 15,
height = NULL,
asp = 2/3,
type = c("png", "emf", "svg"),
res = 300,
pointsize = 10,
align = "c",
newpara = TRUE,
...,
wrd = NULL
)
# S3 method for class 'ggplot'
toWrd(
x,
font = NULL,
width = 15,
height = NULL,
asp = 2/3,
type = c("png", "emf", "svg"),
res = 300,
pointsize = 10,
align = "c",
newpara = TRUE,
...,
wrd = NULL
)
# S3 method for class 'trellis'
toWrd(
x,
font = NULL,
width = 15,
height = NULL,
asp = 2/3,
type = c("png", "emf", "svg"),
res = 300,
pointsize = 10,
align = "c",
newpara = TRUE,
...,
wrd = NULL
)Arguments
- x
Object to be written to Word. Supported types include character vectors and arbitrary objects (converted via
capture-like behavior).- font
Optional font specification (list or object of class
"font")."fix"is the format for R output: for text the paragraph style"R Output", for tables its font, see Fixed format.- ...
Additional arguments passed to methods. A
bookmarkargument is handled by the generic for all methods, see Bookmarks.- wrd
A Word COM object. Defaults to the last active Word instance.
- newpara
logical, end the output with a paragraph mark. Default
TRUEfor tables, pictures and printed output,FALSEfor text. Tables need it, as Word merges directly adjacent tables; the text following a picture starts on a new line with the former alignment.- para
paragraph format
- style
style template name
- bullet
for bullet style
- digits, fmt
number format, passed to
pharos::fm(): for the data.frame method applied to the numeric columns (recycled over them), for matrices and tables to all cells.digitsis the number of decimals (default: as many as the data need),fmta format code such as"%"or aStyle.- naForm, verbose
for
TModtables (alloy::tMod()): passed to itsformat()method, as isdigits.- rowtitle
header of the row name column. Default: the name of the row dimension (
names(dimnames(x))[1]), else empty.- labels
row labels for
TModtables and for the row names of matrices and tables: a named character vectorc(name = "label")(names not found are left as they are, unused labels are ignored, so one map can serve several tables), or a function taking and returning the names. ForTModit applies to coefficients and statistics.- align
character vector, one per column: "l", "r", "c" or "d" (decimal), recycled. Default: first column "l", the others "d" if they hold numbers (also formatted ones like
"-1.2 ***"or"12 (34.5%)"), else "l".- indent
string replacing the leading blanks of the level rows of categorical variables in a
TOnetable (blanks would be trimmed).- legend
logical, write the test legend of a
TOnetable below the table, in a font 2 pt smaller.- halign
alignment of the header cells of decimal columns ("l", "c", "r").
- decpos
numeric, position of the decimal tab in points from the left text boundary of the cell, recycled over the "d" columns.
NULLorNA: the column content is centred on an estimated decimal position.- spacing
paragraph spacing:
before,after(points) andline(multiple of single line spacing).- symbols
TRUEuseswrdSymbolMap(),FALSEnone, or a named character vector (pattern = replacement, perl regex).- na
string used for NA values.
- autofit
column widths:
"window"(default) fits the columns to their content and then the table to the window width,"content"(orTRUE) to the content only,"none"(orFALSE) keeps Word's equal widths.- rownames
logical, write the row names as first column. Default:
TRUEifxhas row names.- freqtitle
header of the frequency column of a 1-dimensional table.
- width, height
size of the picture in cm.
heightdefaults towidth * asp.- asp
aspect ratio height/width used for the default height.
- type
file format:
"png"(default),"emf"(vector, Windows) or"svg"(vector, Word 2016 or later).- res
resolution of png pictures in dpi.
- pointsize
base font size of the plot.
Details
The function inserts text into the current selection of a Word document. Character input is inserted directly, while other objects are first converted to text using an internal capture mechanism.
Formatting options such as paragraph style, font, and bullet lists can be applied via method-specific arguments.
Text: the elements of a character vector become paragraphs;
the last one ends with a paragraph mark only with newpara = TRUE
(or a final "\n"), so that text can be continued. Printed
output (the default method) always ends with one.
TMod tables: the coefficients and, separated by a rule,
the model statistics, all model columns aligned at the decimal point.
The header of the first column is rowtitle.
TOne tables (DescToolsX::tOne()): the column header
"var" is left empty, the group columns are right aligned and the
p-values aligned at the decimal point.
Tables: a 1-dimensional table is written as one column of
frequencies, a 2-dimensional one like a matrix, higher dimensions are
flattened with ftable() first.
Pictures: align is one of "c" (default),
"l" or "r".
Fixed format
Text written with font = "fix", e.g. printed R output, gets the
paragraph style "R Output". If the document (or its template)
has a style of that name, it is used as it is, so it can be designed in
Word. Otherwise it is created as Consolas 10 pt, line spacing 1.25, no
space before and after, without spell checking. Name and defaults can be
changed, also partially:
Bookmarks
With bookmark = "name" the output is enclosed in a Word bookmark.
If the bookmark already exists in the document, its content (text and
tables) is replaced in place. Re-running the same call, e.g. the whole
analysis script, thus updates the report instead of appending to it.
bookmark = TRUE creates a new name ("bmt", for plots
"bmp", followed by 9 digits), returned invisibly. Names must start with a letter and consist
of at most 40 letters, digits and underscores.