Skip to contents

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

Value

One string.

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.

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)
#> [1] "0.80 (95% CI 0.46 to 1.16)"
unlink(c(blocks_file, path))