quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.76k stars 306 forks source link

Feature request: For quarto book qmd files, allow execution order to differ from order appearing in book #3568

Open donboyd5 opened 1 year ago

donboyd5 commented 1 year ago

I have the following common use case:

Thus, I need to calculate the facts in the body-of-the-report chapters before executing the code for the summary chapter, but I want the summary to appear first. It would be great to define an execution order for the files and an appearance order for the files that may be different, so that I could calculate the summary chapter last but have it appear first.

There are workarounds, of course, such as:

Neither of these seems as good as having the ability to separate execution order from appearance order.

Would love to hear if there are better workarounds - or perhaps quarto already has this capability and I haven't found it yet.

Many thanks.

rgaiacs commented 1 year ago

Dear @donboyd5, I have the same use case.

Quarto Document

If you are only targeting HTML output, one great tool is Observable as it build a graph of dependency before execute the code. This allows you to calculate key facts in R, export to Observable and customise the appearance with Observable.

Zip file with minimal working example

Consider

---
title: "Minimal Working Example"
---

# Summary

Key fact is ${key_fact}

# Methods

```{r}
key_fact <- 1 + 1
ojs_define(key_fact = key_fact)

It produces

![Screenshot 2022-12-07 at 11-53-38 Minimal Working Example](https://user-images.githubusercontent.com/1506457/206084480-4809646b-6705-445c-abd6-22be432e5618.png)

Observable is injecting `key_fact`.

If you are targeting PDF or DOCX, Observable will not work. :cry: You are limited to the workarounds that you mentioned.

# Quarto Book

AFAIK, Observable will not work across chapters. :cry: 

[Zip file with minimal working example](https://github.com/quarto-dev/quarto-cli/files/10172663/ojs-book.zip)

One chapter with

Summary

1 + 1

Key fact is ${key_fact}


and another chapter with

Methods

key_fact <- 1 + 1
ojs_define(key_fact = key_fact)


produces

![Screenshot 2022-12-07 at 11-59-02 Minimal Working Example](https://user-images.githubusercontent.com/1506457/206085190-ff3ee955-4555-4a0f-ab7e-d2253d6b155b.png)

Observable silent fail to inject `key_fact` because the variable was defined in another file.
donboyd5 commented 1 year ago

Thank you, @rgaiacs. I did not know about this. I have been experimenting with it. It seems very useful for situations in which I need to pull information forward within a single qmd file. Meanwhile, I'll hope over the longer term differing execution and appearance orders may be added as a feature.

Also, thanks for the reproducible example - allowed me to get up and running quickly.

nickdickinson commented 11 months ago

@donboyd5 Have you considered using quarto includes: https://quarto.org/docs/authoring/includes.html

I haven't myself but I read that it should also deal with this specific use case: "For example, here we include a .qmd that does some data preprocessing that we want shared across multiple documents"