H11 Select columns
Use your current course session. Preparation and course runner · R script ↓
H11 Select columns
Goal: Deliberately narrow the output.
Prerequisites: H10.
A strict contract must match the output. Create a separate two-column product rather than silently violating the original contract.
training_step(
"H11",
{
small_recipe <- dr_recipe() |> dr_step_select(policy_id, premium)
small_contract <- dr_contract(
"policy.amounts",
c(policy_id = "integer", premium = "numeric"),
key = "policy_id"
)
small_product <- dr_product(
"policy.amounts",
policies,
contract = small_contract
) |>
dr_add_recipe(small_recipe)
small_rows <- dr_collect(dr_run(small_product, write = FALSE))
print(small_rows)
stopifnot(identical(names(small_rows), c("policy_id", "premium")))
},
needs = c("H10")
)Expected result: Six rows and two columns.
Task: What would happen with the original three-column contract?
Show solution
The missing cancelled column would block the run. Next, save the complete product.