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_fileruns. A block can call each function thatenvirholds or finds.- block_class, require_block, messages
As in
check_blocks().
Details
make_blocks() does these steps, in this order:
It runs
blocks_filewithsys.source()in a new environment whose parent isenvir. It stops when that environment holds no objectblocks.It stops when
check_blocks()gives a FAIL onblocks. The message gives the rule and the detail of each FAIL.It calls the function of each block on
results, in the order ofblocks. The function of a block of the classblock_classis its elementfn. 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_classMUST 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 asblocksand 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.
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)
#> $estimates
#> term estimate
#> 1 x 1.2
#>
unlink(c(blocks_file, path))