Skip to contents

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

Value

path, invisibly.

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.

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