quarto-dev / quarto-cli

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

Sharing R environment between book chapters #1590

Open andyofsmeg opened 2 years ago

andyofsmeg commented 2 years ago

I'm playing with Quarto Books for the first time having been a R/bookdown user for a while. I have a simple book with two chapters. In the first chapter I define a variable , x, and I want to then use x in my second chapter. In bookdown this works fine. But with Quarto I get, "Error in eval(expr, envir, enclos) : object 'x' not found" when I click render in RStudio. I'm very willing to accept user error, or that independence of chapters is a design choice. But it wasn't something I was expecting.

I've attached a worked example.

What I'm actually more interesting in working out is how to pass params (from a YAML header) from chapter to chapter. Previously I could include something like this in the index file:

---
params:
  hello: world
---

and then access it in all other chapters test_params_quorto.zip .

cderv commented 2 years ago

bookdown defaults to a Merge and Knit approach where all the document are merged before knitting. Other approach Knit and Merge when new_session: true renders each file its own session. More on that in https://bookdown.org/yihui/bookdown/new-session.html

Quarto works in a way similar to the latter where document each run R code in a separate session. See discussion about this in other discussion like https://github.com/quarto-dev/quarto-cli/discussions/1045 and linked ones.

For now, you would need to use some explicit caching and object saving + reloading if you want to share data across R sessions in several files.

Regarding parameters, it is supported in Quarto and should be working in Book format also (https://quarto.org/docs/computations/parameters.html). But what you mean is that the parameter is not accessible in other document right ?

It is possible that we need to add support for parameters defined in _quarto.yml which is not possibly yet I think.

eeholmes commented 2 years ago

issue #1712 would solve if the needed shared environments were in adjacent chapters, so like chapter 8 and chapter 9. You could put the chapters in one qmd file.

andyofsmeg commented 2 years ago

Thanks. An environment option when rendering would be useful. Regarding parameters in a book, yes. I mean the same as with environment variables. I can define within one chapter but they are not passed on. I would like to be able to define parameters centrally and have all chapters be able to access them.

mavam commented 1 year ago

I would like to be able to define parameters centrally and have all chapters be able to access them.

Is this currently possible? Can I pass --execute-params params.yaml with a central params.yaml shared across all chapters?

al-obrien commented 11 months ago

Having this available from _quarto.yml would be very useful, or some kind of meta file; not sure how far away that feature is yet but would be excited to hear about progress on this.

cderv commented 11 months ago

For reference the params topic is related to

jonpeake commented 5 months ago

Hi, has there been any development on having parameters passed from the _quarto.yml file? Alternatively, can anyone suggest a workaround for my situation? I'm trying to make a book with a dynamic title and footer based on runtime-defined parameters (i.e., using execute_params command when rendering), but it doesn't render with the parameters passed through that command. It will only render with the "blank" parameters that I pre-define at the beginning of the index.qmd file (that I assume would've been replaced by the runtime parameter values).

mcanouil commented 5 months ago

@jonpeake The issue is still open and the milestone is set to Future meaning, it is planned but not in a short term timeframe.

Since this is an issue and are asking for something in particular, I suggest you open a discussion and share a concrete small example of your setup, alongside a clear explanation of what your goal is and what you want to achieve.

jonpeake commented 5 months ago

Thank you for the update, I appreciate it. I'll go ahead and set up a discussion post in the meantime.