if (FALSE) {
isolate_example("contain side effects", {
# For more examples, visit
# https://books.ropensci.org/drake/plans.html.
# Create drake plans:
mtcars_plan <- drake_plan(
write.csv(mtcars[, c("mpg", "cyl")], file_out("mtcars.csv")),
value = read.csv(file_in("mtcars.csv"))
)
if (requireNamespace("visNetwork", quietly = TRUE)) {
plot(mtcars_plan) # fast simplified call to vis_drake_graph()
}
mtcars_plan
make(mtcars_plan) # Makes `mtcars.csv` and then `value`
head(readd(value))
# You can use knitr inputs too. See the top command below.
load_mtcars_example()
head(my_plan)
if (requireNamespace("knitr", quietly = TRUE)) {
plot(my_plan)
}
# The `knitr_in("report.Rmd")` tells `drake` to dive into the active
# code chunks to find dependencies.
# There, `drake` sees that `small`, `large`, and `coef_regression2_small`
# are loaded in with calls to `loadd()` and `readd()`.
deps_code("report.Rmd")
# Formats are great for big data: https://github.com/ropensci/drake/pull/977
# Below, each target is 1.6 GB in memory.
# Run make() on this plan to see how much faster fst is!
n <- 1e8
plan <- drake_plan(
data_fst = target(
data.frame(x = runif(n), y = runif(n)),
format = "fst"
),
data_old = data.frame(x = runif(n), y = runif(n))
)
# Use transformations to generate large plans.
# Read more at
# `https://books.ropensci.org/drake/plans.html#create-large-plans-the-easy-way`. # nolint
drake_plan(
data = target(
simulate(nrows),
transform = map(nrows = c(48, 64)),
custom_column = 123
),
reg = target(
reg_fun(data),
transform = cross(reg_fun = c(reg1, reg2), data)
),
summ = target(
sum_fun(data, reg),
transform = cross(sum_fun = c(coef, residuals), reg)
),
winners = target(
min(summ),
transform = combine(summ, .by = c(data, sum_fun))
)
)
# Split data among multiple targets.
drake_plan(
large_data = get_data(),
slice_analysis = target(
analyze(large_data),
transform = split(large_data, slices = 4)
),
results = target(
rbind(slice_analysis),
transform = combine(slice_analysis)
)
)
# Set trace = TRUE to show what happened during the transformation process.
drake_plan(
data = target(
simulate(nrows),
transform = map(nrows = c(48, 64)),
custom_column = 123
),
reg = target(
reg_fun(data),
transform = cross(reg_fun = c(reg1, reg2), data)
),
summ = target(
sum_fun(data, reg),
transform = cross(sum_fun = c(coef, residuals), reg)
),
winners = target(
min(summ),
transform = combine(summ, .by = c(data, sum_fun))
),
trace = TRUE
)
# You can create your own custom columns too.
# See ?triggers for more on triggers.
drake_plan(
website_data = target(
command = download_data("www.your_url.com"),
trigger = "always",
custom_column = 5
),
analysis = analyze(website_data)
)
# Tidy evaluation can help generate super large plans.
sms <- rlang::syms(letters) # To sub in character args, skip this.
drake_plan(x = target(f(char), transform = map(char = !!sms)))
# Dynamic branching
# Get the mean mpg for each cyl in the mtcars dataset.
plan <- drake_plan(
raw = mtcars,
group_index = raw$cyl,
munged = target(raw[, c("mpg", "cyl")], dynamic = map(raw)),
mean_mpg_by_cyl = target(
data.frame(mpg = mean(munged$mpg), cyl = munged$cyl[1]),
dynamic = group(munged, .by = group_index)
)
)
make(plan)
readd(mean_mpg_by_cyl)
})
}
Run the code above in your browser using DataLab