V08 Read an Excel source
Use your current course session. Preparation and course runner · R script ↓
Optional deep dive. Enable only the options named in each block, after preparing its packages or services.
V08a Read a CSV source
Goal: Separate file reading from definition.
Prerequisites: H04; extras.
Create a small reproducible CSV. The source reads it when executed.
training_step(
"V08a",
{
csv_path <- file.path(training_root, "policies.csv")
utils::write.csv(policies, csv_path, row.names = FALSE)
csv_source <- dr_source_file("policies.file", csv_path)
csv_product <- dr_product(
"policies.csv",
csv_source,
contract = policy_contract
)
print(dr_collect(dr_run(csv_product, write = FALSE)))
},
needs = c("H04"),
enabled = training_options$extras,
optional = TRUE
)Expected result: Six rows read from a file.
Task: What if the file changes between definition and execution?
Show solution
The next run reads the changed file. Pinning a fixed release is a separate choice.
V08 Read an Excel source
Goal: Use an appropriate reader.
Prerequisites: H04; readxl, writexl; extras.
writexl only creates the fixture. DataRaft reads Excel through readxl.
training_step(
"V08",
{
xlsx_path <- file.path(training_root, "policies.xlsx")
writexl::write_xlsx(policies, xlsx_path)
xlsx_source <- dr_source_file(
"policies.excel",
xlsx_path,
reader = readxl::read_excel
)
xlsx_product <- dr_product("policies.excel", xlsx_source) |>
dr_add_recipe(
dr_recipe() |> dr_step_mutate(policy_id = as.integer(policy_id))
) |>
dr_add_contract(policy_contract)
print(dr_collect(dr_run(xlsx_product, write = FALSE)))
},
needs = c("H04"),
packages = c("readxl", "writexl"),
enabled = training_options$extras,
optional = TRUE
)Expected result: Policy IDs are explicitly converted to integer.
Task: Why is this conversion needed?
Show solution
Excel does not have R's integer class; imported numbers can be doubles.