Package 'writeit'

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

Help Index


Declare a block of numbers for the paper

Description

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.

Usage

block(fn, format, items = character(0))

Arguments

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 "1.2" or "5%".

format

One string that is not NA and not empty: the name of a formatter.

items

A character vector of "<id>:<item>" strings, without names. An ⁠<id>⁠ holds lower-case letters, digits and -. An ⁠<item>⁠ holds no space and no :. Each string comes once.

Value

A list with the elements fn, format and items, of the class writeit_block. items has no names.

See Also

Other blocks: check_blocks(), house_formatters(), make_blocks(), read_blocks(), txt(), write_blocks()

Examples

b <- block(
  function(results) return(results$estimates),
  format = "ci",
  items = "guide:4a"
)
class(b)

Check a list of blocks

Description

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.

Usage

check_blocks(
  blocks,
  block_class = "writeit_block",
  require_block = TRUE,
  messages = writeit_messages()
)

Arguments

blocks

The object blocks of a blocks file.

block_class

The class of a block that block() declares. A package with its own block function passes its own class.

require_block

TRUE when each block MUST be of the class block_class. With FALSE, a block MAY be a plain function of the results object.

messages

The texts that name a function in a detail, a named list as writeit_messages() gives. The details use its element block.

Details

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.

Value

A data.frame with 2 rows, one for each rule, and the columns rule, status and detail. status is "PASS", "FAIL" or "SKIP".

See Also

Other blocks: block(), house_formatters(), make_blocks(), read_blocks(), txt(), write_blocks()

Examples

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 a results object against the contract

Description

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.

Usage

check_results(x, home = NULL, messages = writeit_messages())

Arguments

x

The results object.

home

NULL, or the folder that holds the files that the manifest names. The rule results_current compares each md5 in the manifest with the file at ⁠file.path(home, <name>)⁠. With NULL, it gives SKIP.

messages

The texts that name a function in a detail, a named list as writeit_messages() gives. The details use its element writer.

Details

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.

Value

A data.frame with 6 rows, one for each rule, and the columns rule, status and detail. status is "PASS", "FAIL" or "SKIP".

See Also

Other results: write_results(), writeit_messages()

Examples

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)))

The house formatters

Description

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.

Usage

house_formatters()

Details

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.

Value

A named list of 5 functions: ci, p, effect, n_pct and mean_sd.

See Also

Other blocks: block(), check_blocks(), make_blocks(), read_blocks(), txt(), write_blocks()

Examples

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 the values of the blocks and write the blocks file

Description

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.

Usage

make_blocks(
  results,
  blocks_file,
  path,
  envir = globalenv(),
  block_class = "writeit_block",
  require_block = TRUE,
  messages = writeit_messages()
)

Arguments

results

The results object, the argument of each block function.

blocks_file

The path of an R file that assigns the object blocks, a named list of blocks.

path

The path of the blocks file to write.

envir

The environment that is the parent of the environment in which blocks_file runs. A block can call each function that envir holds or finds.

block_class, require_block, messages

As in check_blocks().

Details

make_blocks() does these steps, in this order:

  1. It runs blocks_file with sys.source() in a new environment whose parent is envir. It stops when that environment holds no object blocks.

  2. It stops when check_blocks() gives a FAIL on blocks. The message gives the rule and the detail of each FAIL.

  3. 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.

  4. 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.

  5. 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.

Value

path, invisibly.

See Also

Other blocks: block(), check_blocks(), house_formatters(), read_blocks(), txt(), write_blocks()

Examples

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 the blocks file

Description

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.

Usage

read_blocks(path, fake_attribute = "writeit_fake")

Arguments

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 TRUE.

Details

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.

Value

The values: the list that write_blocks() wrote, with its attributes.

See Also

Other blocks: block(), check_blocks(), house_formatters(), make_blocks(), txt(), write_blocks()

Examples

path <- tempfile(fileext = ".qs2")
write_blocks(list(estimates = data.frame(term = "x", estimate = 1.2)), path)
read_blocks(path)
unlink(path)

Give one formatted number of a block

Description

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.

Usage

txt(
  name,
  row = 1,
  col = NULL,
  path,
  blocks_file,
  formatters = list(),
  block_call = "writeit::block",
  fake_attribute = "writeit_fake",
  messages = writeit_messages()
)

Arguments

name

One string: the name of the block.

row

One whole number of 1 or more: the row of the formatted value.

col

NULL, or the name or the number of a column. Give it only when the formatter returns a data.frame. NULL takes the one column of a data.frame with one column.

path

The path of the blocks file that make_blocks() wrote.

blocks_file

The path of the R file that assigns blocks. txt() parses this file and does not run it.

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 blocks_file, such as "writeit::block".

fake_attribute

As in read_blocks().

messages

As in check_results().

Details

txt() does these steps, in this order:

  1. 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.

  2. It reads the values with read_blocks() from path.

  3. It takes the formatter format from formatters, and else from house_formatters().

  4. 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.

  5. 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.

Value

One string.

See Also

Other blocks: block(), check_blocks(), house_formatters(), make_blocks(), read_blocks(), write_blocks()

Examples

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 the blocks file

Description

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.

Usage

write_blocks(values, path)

Arguments

values

A list that is not a data.frame. Each element has a name that is not NA and not empty, and no name is there twice. An empty list is allowed.

path

The path of the blocks file.

Details

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.

Value

path, invisibly.

See Also

Other blocks: block(), check_blocks(), house_formatters(), make_blocks(), read_blocks(), txt()

Examples

path <- tempfile(fileext = ".qs2")
write_blocks(list(estimates = data.frame(term = "x", estimate = 1.2)), path)
read_blocks(path)
unlink(path)

Write a results file with its manifest

Description

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().

Usage

write_results(
  x,
  path,
  version,
  source_md5,
  data_files = NULL,
  packages = session_packages(),
  manifest_extra = list()
)

Arguments

x

The results object: a list that is not a data.frame and has no element manifest.

path

The path of the results file.

version

The version label of the run, for example "v01".

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

NULL, or a data.frame with the columns file, size and mtime, or those and md5. With NULL, the manifest has no element 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 n_rows_data. Each name is not empty, comes once, and is not a core name.

Details

The manifest is a list with these elements, in this order:

  1. writer: ⁠list(package = "writeit", version = <the installed version of writeit>)⁠.

  2. r_version: R.version.string.

  3. packages.

  4. run_date: the date of the run, as "YYYY-MM-DD".

  5. version.

  6. source_md5.

  7. data_files, only when it is not NULL.

  8. 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.

Value

path, invisibly.

See Also

Other results: check_results(), writeit_messages()

Examples

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)

The texts that name a function in a message

Description

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.

Usage

writeit_messages()

Value

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.

See Also

Other results: check_results(), write_results()

Examples

writeit_messages()$writer