I have a targets pipeline that trains multiple models and in the end creates a flexdashboard (or Rmd file) that summarizes the models.
For the summary I have an Rmd file that is included in the targets pipeline using tarchetypes::tar_render()
. The dependency management of the output rmarkdown itself works as expected, the catch however is, that it includes an expanded template Rmd file that summarizes a single model using knitr::knit_expand()
and this template file has additional dependencies that are not caught by targets (ie output depends on varimp1 and 2 in the picture below).
Can I include the dependencies manually or is there a better way to create this dynamically created Rmd file (one section for each model)?
MWE
The below pipeline replicates the problem. The ultimate output is the output.Rmd file that uses the template.Rmd file once for each model. The dependency for this pipeline is this:
Note that the output target does not depend on the varimp1/2 targets (instead the template_file (itself a dependency of output) uses the varimp1/2 targets), therefore the output might not be created as varimp1/2 might not exist or might be out of date.
_targets.R
library(targets)
library(tarchetypes)
# dummy functions
train_model <- function(id) {
return(id)
}
get_var_imp <- function(mdl, seed) {
# note the seed argument is used here only to easily change
# and invalidate the results...
set.seed(seed)
return(data.frame(x = runif(1), y = runif(1)))
}
list(
tar_target(model1, train_model(id = "model1")),
tar_target(model2, train_model(id = "model2")),
tar_target(varimp1, get_var_imp(model1, seed = 1)),
tar_target(varimp2, get_var_imp(model2, seed = 2)),
tar_target(template_file, "template.Rmd", format = "file"),
tar_render(output, "output.Rmd")
)
output.Rmd
---
title: "Output"
output: rmarkdown::html_document
---
# This is a general section
```{R}
# these dependencies are working
model1 <- tar_read(model1)
model2 <- tar_read(model2)
```
For each var model create a new chapter
```{r, echo=FALSE}
models <- c("model1", "model2")
NAME_MAP <- c(
model1 = "A first Model",
model2 = "A second Model"
)
txt <- sapply(models, \(target_name) knitr::knit_expand(tar_read(template_file)))
```
`r paste(knitr::knit(text = txt), collapse = '\n')`
template.Rmd
```{r, echo=FALSE}
# this dependency is picked up because it already exists
# in output.Rmd
mdl <- tar_read_raw("{{target_name}}")
mdl_name <- NAME_MAP[["{{target_name}}"]]
# this dependency is not picked up! =================================
vi <- tar_read_raw(gsub("model", "varimp", "{{target_name}}"))
```
# Model `{{target_name}}` - `r mdl_name`
Feature Imps are
```{r, echo=FALSE}
# or do something with mdl
vi
```
The resulting document will look like this