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.NULLtakes 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:
It parses
blocks_file. The file MUST hold one top-level assignmentblocks <- list(...)with a unique name for each element. The elementnameMUST be a call ofblock_callwith a stringformat.It reads the values with
read_blocks()frompath.It takes the formatter
formatfromformatters, and else fromhouse_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
rowandcol.
txt() stops when one of these steps fails. It also stops in these
cases:
pathis 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.
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)
#> [1] "0.80 (95% CI 0.46 to 1.16)"
unlink(c(blocks_file, path))