| Title: | Reversible Reproducible Documents |
|---|---|
| Description: | Implements a reversible 'R Markdown' to 'Microsoft Word' pipeline. |
| Authors: | Noam Ross [aut, cre] (ORCID: <https://orcid.org/0000-0002-2136-0000>) |
| Maintainer: | Noam Ross <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-05-26 06:19:42 UTC |
| Source: | https://github.com/redoc-dev/redoc |
Implements a reversible 'R Markdown' to 'Microsoft Word' pipeline.
The development repository for the redoc package is found at https://github.com/noamross/redoc. Please file bug reports or other feedback at https://github.com/noamross/redoc/issues
Noam Ross [email protected]
Converts a document originally created with redoc() back to R
Markdown, including changes made to text in MS Word.
dedoc( docx, to = NULL, dir = ".", track_changes = "comments_only", block_missing = "comment", inline_missing = "omit", wrap = getOption("redoc.wrap", 80), overwrite = FALSE, orig_docx = NULL, orig_codefile = NULL, verbose = FALSE )dedoc( docx, to = NULL, dir = ".", track_changes = "comments_only", block_missing = "comment", inline_missing = "omit", wrap = getOption("redoc.wrap", 80), overwrite = FALSE, orig_docx = NULL, orig_codefile = NULL, verbose = FALSE )
docx |
The '.docx“ file to convert |
to |
the filename to write the resulting |
dir |
The directory to write the '.Rmd“ to. Defaults to current working directory |
track_changes |
How to deal with tracked changes and comments in the
|
block_missing, inline_missing
|
What to do about code blocks or inline code whose output has been removed in the editing of the Word document. "restore" attempts to restore the code as close to its original location in the document as possible. "comment" will do so but wrap it in HTML comments. "omit" will not restore the code at all. |
wrap |
The width at which to wrap text. If |
overwrite |
Whether to overwrite an existing file |
orig_codefile, orig_docx
|
The original |
verbose |
whether to print pandoc progress text |
R chunks may be lost in the editing process if using non-Microsoft word processors (e.g. LibreOffice or in copy-and-pasting text into a new document.
A function for testing is the file can be de-rendered. If not, un-knitting
may be attempted with the orig_chunkfile or orig_docx files in dedoc().
is_redoc(docx)is_redoc(docx)
docx |
A path to a |
a logical value
is_redoc(redoc_example_docx())is_redoc(redoc_example_docx())
redoc modifies R Markdown documents prior to knitting to
capture and store document elements that will need to be restored later. It
does this via a function passed to the pre_knit argument of
rmarkdown::output_format(). make_preknitter generates this function from
a list of wrapper functions.
make_preknitter(wrappers = list())make_preknitter(wrappers = list())
wrappers |
a list of wrapper functions. |
rmarkdown does not provide a mechanism to modify the file prior
to knitting, so redoc accomplishes this by reaching up the call stack and
modifying the environment in the rmarkdown::render() function. The
function generated by make_preknitter will do this by generating the
pre-processed R Markdown file and associated list of code chunks, adding these
the list of intermediates for cleanup, and switching the document input to
this pre-processed document.
A function to be used in 'output_format(pre_knit = ...)“
make_preknitter(wrappers = list(htmlcommentwrap, latexwrap))make_preknitter(wrappers = list(htmlcommentwrap, latexwrap))
Wrapper functions are passed to the wrapper= argument of
redoc() to specify what text in R Markdown files should be
captured and restored when de-rendering (in addition to code chunks, inline
code, and YAML blocks). make_wrapper() simplifies creation of these
wrapper functions and returns a function that can be passed to redoc().
Wrappers included in the package, all of which are used by default, are:
| Type of markup | Function | Label | Type |
| HTML Comments | htmlcommentwrap() |
"htmlcomment" |
inline |
| Single-line LaTeX | latexwrap() |
"latex" |
block |
| Pandoc citations | citationwrap() |
"citation" |
inline |
| Raw Blocks | rawblockwrap() |
"rawblock" |
block |
| Raw Spans | rawspanwrap() |
"rawspan" |
inline |
make_wrapper(label, regex, type = c("block", "inline"))make_wrapper(label, regex, type = c("block", "inline"))
label |
the label to use for chunks of this type, e.g., "citation", "table".
These should be unique for each type of wrapper used. |
regex |
A regular expression that identifies the text to be wrapped and
restored, including all delimiters. It will search in a single string with
line breaks. ICU regular expressions are used.
The |
type |
whether the text should be treated as inline or block text. Inlines
are wrapped in |
Some captured text can not be selected by regular expressions, in which case custom functions can be provided to parse out the relevant text. See the vignette "Developing with redoc" for more detail.
A function of class redoc_wrapper
rmarkdown::render( redoc_example_rmd(), output_format = redoc( wrappers = list( htmlcommentwrap, latexwrap))) # This is how each of these functions are defined in the redoc package htmlcommentwrap <- make_wrapper( label = "htmlcomment", regex = "(?s)<!--.*?-->", type = "inline") latexwrap <- make_wrapper( label = "latex", regex = "(?<=[\n\r])\\\\\\w+.*?(?=[\n\r])", type = "block") rawblockwrap <- make_wrapper( label = "rawblock", regex = "(?s)```\\{=\\w+\\}.*?```\\h*", type = "block") rawspanwrap <- make_wrapper( label = "rawspan", regex = "(?s)`[^`]`\\{=\\w+?\\}", type = "inline") citationwrap <- make_wrapper( label = "citation", regex = "(?:@\\w+|\\[.*?-?@\\w+.*?\\](?!\\[\\(\\{))", type = "inline")rmarkdown::render( redoc_example_rmd(), output_format = redoc( wrappers = list( htmlcommentwrap, latexwrap))) # This is how each of these functions are defined in the redoc package htmlcommentwrap <- make_wrapper( label = "htmlcomment", regex = "(?s)<!--.*?-->", type = "inline") latexwrap <- make_wrapper( label = "latex", regex = "(?<=[\n\r])\\\\\\w+.*?(?=[\n\r])", type = "block") rawblockwrap <- make_wrapper( label = "rawblock", regex = "(?s)```\\{=\\w+\\}.*?```\\h*", type = "block") rawspanwrap <- make_wrapper( label = "rawspan", regex = "(?s)`[^`]`\\{=\\w+?\\}", type = "inline") citationwrap <- make_wrapper( label = "citation", regex = "(?:@\\w+|\\[.*?-?@\\w+.*?\\](?!\\[\\(\\{))", type = "inline")
This is a convenience function for testing and development.
pandoc_ast(file, from = NULL, tolist = TRUE)pandoc_ast(file, from = NULL, tolist = TRUE)
file |
the file to convert using pandoc. |
from |
the format to convert from. If |
tolist |
whether to return the AST as an R list. If |
A list containing the structured document
ast <- pandoc_ast(redoc_example_docx())ast <- pandoc_ast(redoc_example_docx())
Format for converting from R Markdown to a Microsoft Word Document that can
be reversed using dedoc() after editing in Word.
redoc( highlight_outputs = FALSE, wrap = getOption("redoc.wrap", 80), margins = NULL, line_numbers = NULL, comment_author = NULL, keep_md = FALSE, wrappers = list(htmlcommentwrap, latexwrap, rawblockwrap, rawspanwrap, cmwrap, citationwrap), diagnostics = TRUE, ... )redoc( highlight_outputs = FALSE, wrap = getOption("redoc.wrap", 80), margins = NULL, line_numbers = NULL, comment_author = NULL, keep_md = FALSE, wrappers = list(htmlcommentwrap, latexwrap, rawblockwrap, rawspanwrap, cmwrap, citationwrap), diagnostics = TRUE, ... )
highlight_outputs |
whether to highlight outputs from chunks and inline code in the final document |
wrap |
when round-tripping the document, at what width to wrap the
markdown output? Set the default with |
margins |
page margin size. Can be a single value or a named vector
with values, |
line_numbers |
either TRUE or list with any of the arguments |
comment_author |
The name to attribute any CriticMarkup tracked
changes to. Defaults to |
keep_md |
whether to keep the intermediate markdown document |
wrappers |
a list of wrapper functions to capture text to protect when
rendering and de-rendering. See |
diagnostics |
Whether to embed diagnostic information in the output file. If TRUE, this will save session information and the current pandoc and (if used) RStudio versions inside the Word document for later bug-checking. |
... |
other parameters passed to |
redoc_diff() produces a diff object comparing the current contents of a Word
document originally generated by redoc() to the original R markdown file
used to create it.
redoc_diff( docx, target = "original", current = "current", track_changes = "comments_only", block_missing = "comment", inline_missing = "omit", wrap = getOption("redoc.wrap", 80), mode = "sidebyside", context = "auto", tar.banner = NULL, cur.banner = NULL, ... )redoc_diff( docx, target = "original", current = "current", track_changes = "comments_only", block_missing = "comment", inline_missing = "omit", wrap = getOption("redoc.wrap", 80), mode = "sidebyside", context = "auto", tar.banner = NULL, cur.banner = NULL, ... )
docx |
Path to an MS Word |
target, current
|
Which versions of the document to compare. One of "original", "roundtrip", or "current". |
track_changes, block_missing, inline_missing
|
Arguments passed to
|
wrap |
Width to wrap text lines when converting from docx to markdown.
If |
mode, context, tar.banner, cur.banner, ...
|
Arguments passed to
|
When an .docx file is created with redoc(), it internally stores the
original R Markdown file as well as a version that is round-tripped to
.docx and back. redoc_diff() de-renders the current .docx to R
Markdown (with dedoc()) and compares against these versions.
A Diff object, which will be displayed in the RStudio
Viewer, a browser, or the console, depending on the context.
Files for examples and testing
redoc_example_rmd() redoc_example_docx() redoc_example_edited_docx()redoc_example_rmd() redoc_example_docx() redoc_example_edited_docx()
redoc_example_rmd() redoc_example_docx() redoc_example_edited_docx()redoc_example_rmd() redoc_example_docx() redoc_example_edited_docx()
Documents produced with redoc() store an copy of the original
.Rmd files used to produce them. This is useful for diffing against the
version created with dedoc(), especially if tracked changes have not been
used.
redoc_extract_rmd( docx, type = c("original", "roundtrip"), dir = ".", to = NULL, overwrite = FALSE )redoc_extract_rmd( docx, type = c("original", "roundtrip"), dir = ".", to = NULL, overwrite = FALSE )
docx |
A path to a word file or a an |
type |
One of |
dir |
The directory to write the '.Rmd“ to. Defaults to current working directory |
to |
the filename to write the resulting |
overwrite |
whether to overwrite existing files |
The path to the extracted .Rmd
redoc_extract_rmd(redoc_example_docx(), dir = tempdir())redoc_extract_rmd(redoc_example_docx(), dir = tempdir())
These functions act on the RStudio text editor and can be used via the
Addins menu or the R console. rountrip_active_file() ("Render and Update")
knits the current file using redoc() replaces the text with text that has
been round-tripped to Word and back. This helps eliminate small formatting
differences.
The dedoc_* functions de-render Word documents (using dedoc()),
and place the results in the current or new file. By default, they will
also display a diff (generated via redoc_diff()) of edits in the word file
compared to its original version a compilation time. If called without
an input file, they will prompt for file selection via the RStudio GUI.
roundtrip_active_file() dedoc_to_active_file(docx = NULL, showdiff = TRUE) dedoc_to_new_file(docx = NULL, showdiff = TRUE)roundtrip_active_file() dedoc_to_active_file(docx = NULL, showdiff = TRUE) dedoc_to_new_file(docx = NULL, showdiff = TRUE)
docx |
The input Word file, originally generated by |
showdiff |
Display a diff of the current version of the document against the rendered version? |