ropensci / drake

An R-focused pipeline toolkit for reproducibility and high-performance computing
https://docs.ropensci.org/drake
GNU General Public License v3.0
1.34k stars 128 forks source link

Drake runtime environment variables #1328

Closed yonicd closed 3 years ago

yonicd commented 3 years ago

Prework

Question

What would you like to know?

Is there a runtime environment variable that can be checked for when drake runs to skip certain chunks in an Rmd document. In the same spirit of {r my chunk, eval = knitr::opts_knit$get("child")}?

Reproducible example

wlandau commented 3 years ago

drake does not set an environment variable to indicate make() or r_make() is running. Is that what you were asking? I'm not sure I understand the use case.

yonicd commented 3 years ago

i have this type of rmd

---
title: "Analysis"
author: "Jonathan Sidi"
date: "`r Sys.Date()`"
output: 
  bookdown::html_document2: default
  bookdown::word_document2:
    fig_width: 6
    fig_height: 6
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
  echo = FALSE,
  message = FALSE,
  warning = FALSE, 
  fig.path = '../../deliv/figures/',
  fig.topcaption=TRUE
  )
library(officedown)
library(magrittr)
library(ggplot2)
library(patchwork)
library(drake)
source(here::here('script/utils.R'))

...


the _drake.R file has it in it too

```r
library(magrittr)
library(drake)

source(here::here('script/utils.R'))

source(here::here('script/plan.R'))

drake_config(plan, verbose = 2)

I want to be able to run this file both standalone and through drake.

If I run this rmd standalone it runs ok and outputs the html in the directory where the file is saved. But if I try to run it in a drake plan it breaks because of the file is sourced twice.

if i set eval=FALSE then the Rmd isn't standalone anymore.

If there was a way to know if drake is in runtime then I could add a flag in the rmd chunk.

Does this description make sense?

wlandau commented 3 years ago

Seems like the sort of thing that should work. Which file is sourced twice? What kinds of things are in the rest of the Rmd? Does the Rmd call r_make()? Because then that would definitely cause problems. A report can wrap around drake or run within drake, but not both.

yonicd commented 3 years ago

the script/utils.R which is a file with helper functions that are placed in the runtime envir. I wanted to source it in _drake.R because I use the functions in multiple targets

when lock_envir=TRUE I get this error (map_sas_drive is the first function in that file)

Quitting from lines 34-35 (periods.Rmd) 
Error : target period_report failed.
diagnose(period_report)$error$message:
  cannot change value of locked binding for 'map_sas_drive'. 
Please read the "Self-invalidation" section of the make() help file.
diagnose(period_report)$error$calls:
  base::source(here::here("script/utils.R"))
  base::withVisible(eval(ei, envir))
  base::eval(ei, envir)
  base::eval(ei, envir)
Error: callr subprocess failed: target period_report failed.
diagnose(period_report)$error$message:
  cannot change value of locked binding for 'map_sas_drive'. 
Please read the "Self-invalidation" section of the make() help file.
diagnose(period_report)$error$calls:
  base::source(here::here("script/utils.R"))
  base::withVisible(eval(ei, envir))
  base::eval(ei, envir)
  base::eval(ei, envir)
Type .Last.error.trace to see where the error occured
wlandau commented 3 years ago

Ah, I see what you mean now. I recommend two alternatives. Either:

  1. Just disable environment locking, or
  2. Move that part of the computation out of the report and into new targets in the plan. drake’s opinion (and my own) is that R Markdown should do as little work as possible: just load targets and annotate them with descriptive prose. So there ideally won’t be a need to source the function script again inside the report.