| Title: | File Contract Between an Analysis and Its Paper |
|---|---|
| Description: | Defines the files that pass from an analysis to its paper. The analysis writes a results file with a manifest, and a check reads the results and the manifest. Blocks turn the results into a file of numbers, and the text of the paper reads its numbers from that file. |
| Authors: | Richard Aubrey White [aut, cre, cph] (ORCID: <https://orcid.org/0000-0002-6747-1726>) |
| Maintainer: | Richard Aubrey White <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2026.12.3 |
| Built: | 2026-12-03 20:11:54 UTC |
| Source: | https://github.com/raubreywhite/writeit |
block() joins a function of the results object to the name of the
formatter that formats its value. make_blocks() evaluates the function
and writes its raw value to the blocks file.
block(fn, format, items = character(0))block(fn, format, items = character(0))
fn |
A function of one argument, the results object. It MUST return
a data.frame of numbers. A character or a factor column MAY hold labels,
but no cell of it holds a formatted number, such as |
format |
One string that is not |
items |
A character vector of |
A list with the elements fn, format and items, of the class
writeit_block. items has no names.
Other blocks:
check_blocks(),
house_formatters(),
make_blocks(),
read_blocks(),
txt(),
write_blocks()
b <- block( function(results) return(results$estimates), format = "ci", items = "guide:4a" ) class(b)b <- block( function(results) return(results$estimates), format = "ci", items = "guide:4a" ) class(b)
check_blocks() checks the object blocks of a blocks file on 2 rules.
It reports each rule as PASS, FAIL or SKIP, and it does not print or stop.
check_blocks( blocks, block_class = "writeit_block", require_block = TRUE, messages = writeit_messages() )check_blocks( blocks, block_class = "writeit_block", require_block = TRUE, messages = writeit_messages() )
blocks |
The object |
block_class |
The class of a block that |
require_block |
|
messages |
The texts that name a function in a detail, a named list
as |
The 2 rules, in the order of the rows:
blocks_list: blocks is a list with 1 or more elements. Each element
has a name that is not NA and not empty, and no name is there twice.
Each element is a function or an object of the class block_class.
NULL gives FAIL. The detail names each fault.
block_class: with require_block = TRUE, each element is of the
class block_class, and the detail names each element that is not.
With require_block = FALSE, the rule gives PASS and says that plain
functions are allowed. With require_block = TRUE, it gives SKIP when
blocks is not a list with 1 or more elements.
A detail names an element by its name, or as element <i> when its name
is NA or empty.
A data.frame with 2 rows, one for each rule, and the columns
rule, status and detail. status is "PASS", "FAIL" or
"SKIP".
Other blocks:
block(),
house_formatters(),
make_blocks(),
read_blocks(),
txt(),
write_blocks()
blocks <- list( estimates = block(function(results) return(results$estimates), "ci"), counts = function(results) return(results$counts) ) check_blocks(blocks) check_blocks(blocks, require_block = FALSE)blocks <- list( estimates = block(function(results) return(results$estimates), "ci"), counts = function(results) return(results$counts) ) check_blocks(blocks) check_blocks(blocks, require_block = FALSE)
check_results() checks a results object against the contract of the
results file. It reports each of its 6 rules as PASS, FAIL or SKIP, and it
does not print or stop.
check_results(x, home = NULL, messages = writeit_messages())check_results(x, home = NULL, messages = writeit_messages())
x |
The results object. |
home |
|
messages |
The texts that name a function in a detail, a named list
as |
A list with no element manifest is a legacy result: something wrote it
before the manifest. On a legacy result, the rules manifest_data,
manifest_source and results_current give SKIP. The rule
element_names gives FAIL on a fault in the names and SKIP otherwise.
SKIP means that the rule could not compare. It is never a PASS.
The 6 rules, in the order of the rows:
element_names: x is a list and not a data.frame. Each element has a
name. Each name is syntactic, so make.names() does not change it, and
no name is there twice. The element manifest is there at most once.
allowed_objects: each element at each depth is a data.frame, an
atomic vector of any class, NULL, or a list of any class. The walk
goes into each list, each column of a data.frame and each attribute,
and it writes the path of an attribute as attr(x$a, "terms"). It
skips the attributes that R and data.table set: names, dim,
dimnames, row.names, class, levels, tzone, units,
comment, sorted, index and .internal.selfref.
The rule fails these objects, and the detail names the path, the class and a fix of each:
a function, an environment, a call, a formula, a name and an external pointer.
an S4 object and an R6 object.
a fitted model of the class lm, glm, gam, merMod, brmsfit,
coxph or lavaan.
no_row_per_person: no element at any depth has one value for each row
of the data. The walk skips manifest and exclusions. exclusions is
a table with one row for each step, so k steps and k people would fail
it. The rule reads the row counts in the optional manifest fields
n_rows_data, n_rows_before_exclusions, n_rows_after_exclusions
and n_rows_lists. For each count n of 2
or more, it fails these objects:
a data.frame with n rows.
an atomic vector of length n.
a matrix or an array with a dimension n.
When there is no count of 2 or more, it gives PASS and says that it checked nothing.
manifest_data: when manifest$data_files is there, it MUST be a
data.frame with the columns file, size and mtime, or those and
md5. Its values MUST obey these rules:
each file is a name that is not empty and comes once.
each size is a number of 0 or more.
each mtime is a number.
each md5 is 32 lower-case hex digits or NA.
Without data_files, the rule gives PASS.
manifest_source: manifest$source_md5 is a named character vector of
one or more md5 values, each 32 lower-case hex digits. Each name is
there once and is not empty.
results_current: each md5 of source_md5, and each md5 of
data_files that is not NA, is the md5 of its file in home. The
rule gives FAIL and names each file that differs. It gives SKIP when
home is NULL, when a file is not in home, or when the manifest
records no md5. It gives PASS only when it compared each md5.
A data.frame with 6 rows, one for each rule, and the columns
rule, status and detail. status is "PASS", "FAIL" or
"SKIP".
Other results:
write_results(),
writeit_messages()
x <- list( manifest = list( version = "v01", source_md5 = c(run.R = "fedcba9876543210fedcba9876543210"), data_files = data.frame(file = "main/d.csv", size = 120, mtime = 1.8e9), n_rows_data = 100L ), estimates = data.frame(term = "x", estimate = 1.2), p_interaction = 0.04 ) check_results(x) # A legacy result, written before the manifest. check_results(list(estimates = data.frame(term = "x", estimate = 1.2)))x <- list( manifest = list( version = "v01", source_md5 = c(run.R = "fedcba9876543210fedcba9876543210"), data_files = data.frame(file = "main/d.csv", size = 120, mtime = 1.8e9), n_rows_data = 100L ), estimates = data.frame(term = "x", estimate = 1.2), p_interaction = 0.04 ) check_results(x) # A legacy result, written before the manifest. check_results(list(estimates = data.frame(term = "x", estimate = 1.2)))
house_formatters() gives the formatters that txt() uses when the
project gives no formatter of the same name. Each formatter takes the raw
value of a block, a data.frame, and returns one string for each row.
house_formatters()house_formatters()
The list holds 5 formatters. Each name is the format of a block().
The examples show the string for one row:
ci, of the columns est, lo and hi: "0.80 (95% CI 0.46 to 1.16)".
p, of the column p: "p < 0.001" or "p = 0.047".
effect, of the columns est, lo, hi, p and an optional unit:
"(0.80 points, 95% CI 0.46 to 1.16, p < 0.001)".
n_pct, of the columns n and total: "1,037 (68.7%)".
mean_sd, of the columns mean and sd: "30.7 (4.8)".
A numeric column digits gives the decimals of each row. The defaults
are 2 for ci and effect, and 1 for n_pct and mean_sd. A p-value
has 3 decimals. The count of n_pct has no decimals and a comma as the
thousands mark.
A negative number starts with the minus sign U+2212. A number that rounds
to zero has no sign. A row with an NA in a column that the formatter
uses gives NA. A formatter stops when a column that it needs is absent
or not numeric. It also stops when a digits value is not a whole number
of 0 or more.
A named list of 5 functions: ci, p, effect, n_pct and
mean_sd.
Other blocks:
block(),
check_blocks(),
make_blocks(),
read_blocks(),
txt(),
write_blocks()
house_formatters()$ci(data.frame(est = 0.8, lo = -0.0712, hi = 1.16)) house_formatters()$n_pct(data.frame(n = 1037, total = 1509))house_formatters()$ci(data.frame(est = 0.8, lo = -0.0712, hi = 1.16)) house_formatters()$n_pct(data.frame(n = 1037, total = 1509))
make_blocks() evaluates each block of a blocks file on a results object.
It writes the raw values with write_blocks() only when each block gives
a valid value.
make_blocks( results, blocks_file, path, envir = globalenv(), block_class = "writeit_block", require_block = TRUE, messages = writeit_messages() )make_blocks( results, blocks_file, path, envir = globalenv(), block_class = "writeit_block", require_block = TRUE, messages = writeit_messages() )
results |
The results object, the argument of each block function. |
blocks_file |
The path of an R file that assigns the object |
path |
The path of the blocks file to write. |
envir |
The environment that is the parent of the environment in
which |
block_class, require_block, messages
|
As in |
make_blocks() does these steps, in this order:
It runs blocks_file with sys.source() in a new environment whose
parent is envir. It stops when that environment holds no object
blocks.
It stops when check_blocks() gives a FAIL on blocks. The message
gives the rule and the detail of each FAIL.
It calls the function of each block on results, in the order of
blocks. The function of a block of the class block_class is its
element fn. A plain function is the block itself. After each call,
it sets the options of R back to the options before the call.
It stops at the first block that gives an error or an invalid value.
A plain function MUST return a data.frame or a character vector. A
block of the class block_class MUST return a data.frame. No
character or factor cell of that data.frame holds a formatted number,
such as "1.2", "5%" or "p < 0.05". The message gives the number and the
name of the block.
It calls write_blocks() with a list of the raw values, named as
blocks and in its order.
A stop in steps 1 to 4 comes before the write, so make_blocks() then
writes no file. No formatter runs: the blocks file holds raw values.
path, invisibly.
Other blocks:
block(),
check_blocks(),
house_formatters(),
read_blocks(),
txt(),
write_blocks()
blocks_file <- tempfile(fileext = ".R") writeLines( c( "blocks <- list(", " estimates = writeit::block(", " function(results) return(results$estimates),", " format = \"ci\"", " )", ")" ), blocks_file ) results <- list(estimates = data.frame(term = "x", estimate = 1.2)) path <- tempfile(fileext = ".qs2") make_blocks(results, blocks_file, path) read_blocks(path) unlink(c(blocks_file, path))blocks_file <- tempfile(fileext = ".R") writeLines( c( "blocks <- list(", " estimates = writeit::block(", " function(results) return(results$estimates),", " format = \"ci\"", " )", ")" ), blocks_file ) results <- list(estimates = data.frame(term = "x", estimate = 1.2)) path <- tempfile(fileext = ".qs2") make_blocks(results, blocks_file, path) read_blocks(path) unlink(c(blocks_file, path))
read_blocks() reads the values that write_blocks() wrote. In a knitr
document, fake values put the mark **FAKE DATA** at the top of the
output.
read_blocks(path, fake_attribute = "writeit_fake")read_blocks(path, fake_attribute = "writeit_fake")
path |
The path of the blocks file. |
fake_attribute |
The name of the attribute that marks fake values.
The values are fake when this attribute of the list is |
When the values are fake and knitr renders a document,
read_blocks() adds a knitr document hook. The hook puts
**FAKE DATA** at the top of the output, after the YAML header when the
output starts with one. It keeps the document hook that was there.
The knitr option paste0(fake_attribute, "_marked") records the hook, so
a document gets the mark once, however many reads it does. knitr sets its
options and hooks back at the end of each document. Outside knitr, and on
values that are not fake, read_blocks() only reads.
The values: the list that write_blocks() wrote, with its
attributes.
Other blocks:
block(),
check_blocks(),
house_formatters(),
make_blocks(),
txt(),
write_blocks()
path <- tempfile(fileext = ".qs2") write_blocks(list(estimates = data.frame(term = "x", estimate = 1.2)), path) read_blocks(path) unlink(path)path <- tempfile(fileext = ".qs2") write_blocks(list(estimates = data.frame(term = "x", estimate = 1.2)), path) read_blocks(path) unlink(path)
txt() formats the raw value of a block from the blocks file and returns
one cell as a string. The text of a paper calls it for each number.
txt( name, row = 1, col = NULL, path, blocks_file, formatters = list(), block_call = "writeit::block", fake_attribute = "writeit_fake", messages = writeit_messages() )txt( name, row = 1, col = NULL, path, blocks_file, formatters = list(), block_call = "writeit::block", fake_attribute = "writeit_fake", messages = writeit_messages() )
name |
One string: the name of the block. |
row |
One whole number of 1 or more: the row of the formatted value. |
col |
|
path |
The path of the blocks file that |
blocks_file |
The path of the R file that assigns |
formatters |
The project formatters: a named list or an environment of functions. A project formatter wins over the house formatter of the same name. |
block_call |
One string: the function that declares a block in
|
fake_attribute |
As in |
messages |
As in |
txt() does these steps, in this order:
It parses blocks_file. The file MUST hold one top-level assignment
blocks <- list(...) with a unique name for each element. The element
name MUST be a call of block_call with a string format.
It reads the values with read_blocks() from path.
It takes the formatter format from formatters, and else from
house_formatters().
It calls the formatter on the raw value of name. The formatter MUST
return a data.frame, or a character vector with one string for each
row of the raw value.
It returns the cell of row and col.
txt() stops when one of these steps fails. It also stops in these
cases:
path is absent.
The values hold no element name.
The raw value holds a formatted number, as in make_blocks().
The cell is not a string.
txt() formats at each call, so a change of a formatter needs no new
blocks file.
One string.
Other blocks:
block(),
check_blocks(),
house_formatters(),
make_blocks(),
read_blocks(),
write_blocks()
blocks_file <- tempfile(fileext = ".R") writeLines( c( "blocks <- list(", " effect = writeit::block(", " function(results) return(results$effect),", " format = \"ci\"", " )", ")" ), blocks_file ) results <- list(effect = data.frame(est = 0.8, lo = 0.46, hi = 1.16)) path <- tempfile(fileext = ".qs2") make_blocks(results, blocks_file, path) txt("effect", path = path, blocks_file = blocks_file) unlink(c(blocks_file, path))blocks_file <- tempfile(fileext = ".R") writeLines( c( "blocks <- list(", " effect = writeit::block(", " function(results) return(results$effect),", " format = \"ci\"", " )", ")" ), blocks_file ) results <- list(effect = data.frame(est = 0.8, lo = 0.46, hi = 1.16)) path <- tempfile(fileext = ".qs2") make_blocks(results, blocks_file, path) txt("effect", path = path, blocks_file = blocks_file) unlink(c(blocks_file, path))
write_blocks() writes a named list of block values to path with
qs2::qs_save(). It is the only function of writeit that writes a
blocks file.
write_blocks(values, path)write_blocks(values, path)
values |
A list that is not a data.frame. Each element has a name
that is not |
path |
The path of the blocks file. |
write_blocks() calls qs2::qs_save(values, path) with the defaults of
qs2. The file keeps the attributes of values, so read_blocks() reads
back an object identical to values.
path, invisibly.
Other blocks:
block(),
check_blocks(),
house_formatters(),
make_blocks(),
read_blocks(),
txt()
path <- tempfile(fileext = ".qs2") write_blocks(list(estimates = data.frame(term = "x", estimate = 1.2)), path) read_blocks(path) unlink(path)path <- tempfile(fileext = ".qs2") write_blocks(list(estimates = data.frame(term = "x", estimate = 1.2)), path) read_blocks(path) unlink(path)
write_results() adds a manifest to a results object and writes the
object to path with qs2::qs_save(). It writes no file when the object
with its manifest gives a FAIL in check_results().
write_results( x, path, version, source_md5, data_files = NULL, packages = session_packages(), manifest_extra = list() )write_results( x, path, version, source_md5, data_files = NULL, packages = session_packages(), manifest_extra = list() )
x |
The results object: a list that is not a data.frame and has no
element |
path |
The path of the results file. |
version |
The version label of the run, for example |
source_md5 |
A named character vector of the md5 of each source file of the run. Each name is the path of a file. |
data_files |
|
packages |
A named character vector of package versions. The default gives the version of each package that this R session loaded. |
manifest_extra |
A named list of more manifest fields, for example
|
The manifest is a list with these elements, in this order:
writer: list(package = "writeit", version = <the installed version of writeit>).
r_version: R.version.string.
packages.
run_date: the date of the run, as "YYYY-MM-DD".
version.
source_md5.
data_files, only when it is not NULL.
The elements of manifest_extra, in their order.
The first 7 names are the core names. manifest_extra MUST NOT use one.
An empty manifest_extra adds no element.
write_results() stops before it writes when one of these is true:
x is not a list, or is a data.frame.
x holds an element manifest.
manifest_extra is not a list with a unique, non-empty name for each
element.
manifest_extra uses a core name.
check_results() gives a FAIL on x with its manifest. The message
gives the rule and the detail of each FAIL.
The function writes to a temporary file in the folder of path and then
renames that file to path. When the rename fails, it removes the
temporary file and stops.
path, invisibly.
Other results:
check_results(),
writeit_messages()
path <- tempfile(fileext = ".qs2") x <- list(estimates = data.frame(term = "x", estimate = 1.2)) write_results( x, path, version = "v01", source_md5 = c(run.R = "fedcba9876543210fedcba9876543210"), manifest_extra = list(n_rows_data = 100L) ) names(qs2::qs_read(path)$manifest) unlink(path)path <- tempfile(fileext = ".qs2") x <- list(estimates = data.frame(term = "x", estimate = 1.2)) write_results( x, path, version = "v01", source_md5 = c(run.R = "fedcba9876543210fedcba9876543210"), manifest_extra = list(n_rows_data = 100L) ) names(qs2::qs_read(path)$manifest) unlink(path)
writeit_messages() gives the texts that writeit puts in a message or a
finding when it names a program, a function or a file. A package that
calls writeit passes its own list, so that each message names that
package.
writeit_messages()writeit_messages()
A named list of 6 strings:
program: the program that writes the results file.
writer: the function that writes the results file.
block: the function that declares a block.
make_blocks: the function that makes the values of the blocks.
read_blocks: the function that reads the values of the blocks.
formatters_file: the file that holds the project formatters.
Other results:
check_results(),
write_results()
writeit_messages()$writerwriteit_messages()$writer