Skip to contents

code_to_plan(), plan_to_code(), and plan_to_notebook() together illustrate the relationships between drake plans, R scripts, and R Markdown documents. In the file generated by plan_to_code(), every target/command pair becomes a chunk of code. Targets are arranged in topological order so dependencies are available before their downstream targets. Please note:

  1. You are still responsible for loading your project's packages, imported functions, etc.

  2. Triggers disappear.

Usage

plan_to_code(plan, con = stdout())

Arguments

plan

Workflow plan data frame. See drake_plan() for details.

con

A file path or connection to write to.

Examples

plan <- drake_plan(
  raw_data = read_excel(file_in("raw_data.xlsx")),
  data = raw_data,
  hist = create_plot(data),
  fit = lm(Ozone ~ Temp + Wind, data)
)
file <- tempfile()
# Turn the plan into an R script a the given file path.
plan_to_code(plan, file)
# Here is what the script looks like.
cat(readLines(file), sep = "\n")
#> raw_data <- read_excel(file_in("raw_data.xlsx"))
#> data <- raw_data
#> fit <- lm(Ozone ~ Temp + Wind, data)
#> hist <- create_plot(data)
# Convert back to a drake plan.
code_to_plan(file)
#> # A tibble: 4 × 2
#>   target   command                             
#>   <chr>    <expr_lst>                          
#> 1 raw_data read_excel(file_in("raw_data.xlsx"))
#> 2 data     raw_data                            
#> 3 fit      lm(Ozone ~ Temp + Wind, data)       
#> 4 hist     create_plot(data)