Export a recorded R document as ordinary R or a clean Peter script
Source:R/doc-export.R
gptr_export.RdReads a synchronized .R history without executing it, asking a model or recovering
pending writes. In "r" format the original Peter statements become comments and their
recorded R stays executable. In "peter" format calls stay active and recorded R becomes
comments, so it is not executed a second time. User R and comments keep their source order.
Usage
gptr_export(file, to, format = c("r", "peter"))Details
Only bare or symbol-assigned literal Peter calls and Peter-only native pipelines
are admitted; a pipeline may start from a session handle an earlier Peter statement assigned,
as the later turns of a console transcript do, and after its prompt a call may name objects
without argument names, as a console turn's @objects are written. Every named argument must
be one literal (a string, number, logical or NULL), so a call with agents = list(...),
budget = list() or envir = new.env() is refused, and the refusal names that argument.
A harness definition, <name> = gptr_tailor(...), is ordinary code. Distinct statements sharing
a line (including semicolon-separated statements), mixed pipelines, nested or computed calls,
pending writes, missing or edited blocks, secret markers and interpolated-input hashes are
refused. An undone block that no statement owns, a console turn that /undo or /retry undid
under its #~ statement, is left out. Code that calls a peter$ member, such as
peter$sh(), exports as ordinary R; code that calls peter() is refused.
Raw exports also refuse ordinary code that reads a session handle after its Peter statement
and before ordinary code assigns that name again, and a function that names any session handle
of the document, wherever it is defined, as it reads the name when called. The Peter format
retains such ordinary references, since its calls remain active.
A top-level gptr_doc() whose string literal names the source (relative to the source's
directory or the working directory) becomes a comment in raw R and binds the destination in
the Peter format, so its calls record there: in the source's directory only the file name
changes, elsewhere the literal becomes the destination's full path. It must be alone on its
lines. Other gptr_doc() calls are kept.
Feedback follows each pipeline operand when calls end on separate lines and only its
pipe operator or a comment follows the closing call. Feedback follows the next operand's
indentation (at most 74 characters); other layouts keep feedback after the complete
statement. Peter calls already show their prompts, so only raw R adds prompt comments.
Long raw prompt comments wrap at eighty characters. Strip the recorded-prompt prefix from
the first line and the two-character comment prefix from continuations, then join without
separators to recover the complete escaped R string literal.
A Peter-only pipeline on one line keeps grouped feedback after the statement.
Call source is never reformatted.
A completed-turn identity and matching source hashes establish a recorded snapshot, not
verified execution or a complete session. Ordinary console input and record = FALSE
effects cannot be reconstructed. External files, objects and package versions must still
be supplied. Remaining user code may use gptr or perform network and filesystem operations.
Captured #> output is limited to twelve lines per block and eighty characters per line;
outputs are comments. Neither format keeps a console turn's #< lines (what the console sent
with its prompt, see gptr_doc()), so an exported call is asked without them. Encoding, line
endings and the final newline follow the input file.
A UTF-8 byte order mark is preserved; source that exported file with
source(to, encoding = "UTF-8-BOM").
Examples
input = tempfile(fileext = ".R")
output = tempfile(fileext = ".R")
writeLines(c("x = 1", "# Keep ordinary code and comments.", "y = x + 1"), input)
gptr_export(input, output)
readLines(output)
#> [1] "# Exported recorded R and ordinary source; not executed or validated."
#> [2] "# Earlier console input and unrecorded tool effects are not recovered."
#> [3] "# Review code, external inputs and package versions before running."
#> [4] ""
#> [5] "x = 1"
#> [6] "# Keep ordinary code and comments."
#> [7] "y = x + 1"
unlink(c(input, output))