rmd2word {accessr} | R Documentation |
Converts R markdown code to Word and PDF documents
Description
Creates Word documents from input R markdown documents. On a Windows
Operating System, accessible PDF documents may be created from these Word
files if the software
OfficeToPDF is installed.
The convenience function install_otp
can be used to install
this software. Zip archives of the Word and/or PDF files may be created.
Usage
rmd2word(
x,
doc = "accessr",
pdf = isTRUE(.Platform$OS.type == "windows"),
dir,
zip = if (length(x) == 1 & !add) FALSE else TRUE,
add = FALSE,
quiet = TRUE,
rm_word = FALSE,
rm_pdf = FALSE,
inc_word = FALSE,
params = NULL,
...
)
Arguments
x |
A character vector containing the names (no extension) of
the |
doc |
An optional character vector (including the file
extension) to specify template Word documents on which to base the style
of the respective output Word documents. This determines what is passed as
the argument |
pdf |
A logical scalar. Should |
dir |
A path to the directory in which the file |
zip |
A logical scalar or character vector indicating whether PDF
files should be put into a zip archive. If |
add |
A logical scalar that determines what happens if the output
zip file already exists. If |
quiet |
A logical scalar. Passed to |
rm_word |
A logical scalar. If |
rm_pdf |
A logical scalar. If |
inc_word |
A logical scalar. If |
params |
A list of named parameters to pass as the argument
|
... |
Additional arguments passed to
|
Details
Information such as title
, author
, lang
etc in
the YAML header in the Rmd file are used but output
is ignored.
The simplest setup is to have the .Rmd
files in the current
working directory, in which case rmd2word()
will create Word documents
from all these Rmd files, but the .Rmd
files may be in different
directories.
It is possible to have the .Rmd
files in different
directories, but any non-"default"
values in doc
must be
such that the reference_docx
argument of
word_document
finds a template Word file.
If the template is in the same directory as its respective .Rmd
component in x
then the filename, e.g. "template.docx"
will
suffice. Otherwise, a path to the template should be given, either
relative to the directory in which the .Rmd
file sits, or an
absolute path.
For information on how to create a template Word document see Richard Layton's guide Happy collaboration with Rmd to docx.
There are some built-in template options:
-
doc = "officedown"
: usesrdocx_document
's default, -
doc = "accessr"
: similar to"officedown"
but with narrower margins and black text for titles and darker hyperlinks, to avoid contrast issues, -
doc = "18"
: like"accessr"
but with 18pt text, -
doc = "exam"
: creates a Word file with a header "Examination paper for STAT0002" on the left and "Page x of n" on the right.
To use your own template(s), provide their filename(s).
A component equal to "officedown"
chooses
rdocx_document
's default.
A component equal to "accessr"
chooses accessr
's internal
template file, which has narrower margins and darker blue fonts for titles
and hyperlinks, to avoid contrast issues. To use your own template(s),
provide their filenames. See Details for more information.
The render
function creates a Word file from
each input .Rmd
file. Then
OfficeToPDF is used to
convert the Word file to a PDF file. The file OfficeToPDF.exe
needs to be downloaded from the
OfficeToPDF
releases page and placed in the directory specified by the argument
dir
, or in a directory that is in the list returned by
searchpaths
.
If OfficeToPDF.exe
cannot be found then an error is thrown.
A warning will be given if any of the PDF files could not be produced.
This will occur if there is an existing PDF file of the same name open
in another application.
Value
In addition to creating the Word and PDF files, and perhaps zip files, a list containing the following vector components is returned invisibly:
error_codes |
If |
files |
(absolute) paths and file names of all files created. |
zips |
(relative) paths and names of all zip files created (if
|
References
Layton, Richard. (2015) Happy collaboration with Rmd to docx. R Markdown from RStudio article. https://rmarkdown.rstudio.com/articles_docx.html
See Also
install_otp
to install
OfficeToPDF.
rmd2many
, rmd2ioslides
,
rmd2slidy
, rmd2html
for other output formats.
The accessr package page on Github.
Examples
# Create a Word file from example.Rmd
got_hux <- requireNamespace("huxtable", quietly = TRUE)
got_flex <- requireNamespace("flextable", quietly = TRUE)
got_pandoc <- rmarkdown::pandoc_available("1.14")
got_all <- got_hux && got_flex && got_pandoc
# This example needs packages huxtable and flextable
# We pass pdf = FALSE because OfficeToPDF is needed to convert Word to PDF
# and this is only relevant on Windows Operating System.
if (got_all) {
ex_file <- system.file(package = "accessr", "examples", "example.Rmd")
file.copy(ex_file, tdir <- tempdir(check = TRUE), overwrite = TRUE)
ex_file <- list.files(tdir, pattern = "example.Rmd", full.names = TRUE)
ex_file <- sub(".Rmd", "", ex_file)
rmd2word(ex_file, pdf = FALSE)
}