Conditional Chunk Execution #3260
Replies: 6 comments 11 replies
-
Your suspicion is correct, and unfortunately that's not currently possible. It's on our radar to support a language-agnostic way for quarto to communicate things like output format, but it doesn't currently exist. |
Beta Was this translation helpful? Give feedback.
-
Any news on this? For me that's now a hard requirement for the next paper (i.e. right now) - want to move to Quarto from Org-Mode, where I can do everything, but need to get all colleagues to Emacs. Quarto is great, but not quite as complete for "write-once, publish anywhere" literate programming/reproducible publications. If there isn't a code-block option, is there a trick to set a variable in the headers, so I can do a conditional execution within the code block? Basically, what @agerlach asked. |
Beta Was this translation helpful? Give feedback.
-
This will definitely be a useful feature when it's available! In the meantime, here is a hack that... might work? It relies up on the behavior that execution options specifically within a cell override global execution options specified in document YAML.
Your MWE would look like:
This should lead to the following behavior.
I haven't tried this so I can't verify that it'd work. It counts on the fact that conditional formatting works for custom formats. Reading the docs, I can't tell if that's true or not. |
Beta Was this translation helpful? Give feedback.
-
My current trivial solution is to create two options of defining the same commands and commenting out the irrelevant one as needed. It would be nice to have these in separate files, only called as needed for more complex documents, but I have a functional solution that is likely to remain functional. |
Beta Was this translation helpful? Give feedback.
-
I'm similarly trying to conditionally run chunks and have come to the following "solution" using For R:
For OJS, same first step:
Now, to hide the output we need some CSS, and to assign a CSS class to the cell. First, assign the class by using div fences and some inline R:
This means that the div will be class But the rendering also throws in some raw code (another div fence and some javascript?
Again, this only partially stops evaluation (not really sure how much is being stopped, so beware! |
Beta Was this translation helpful? Give feedback.
-
I have a qmd doc rendered in both html and docx, and I wanted to include a DT datatable in the html output
what worked was to include the problematic part of the code in an tried also to have the whole chunk evaluated or not based on the same condition ( |
Beta Was this translation helpful? Give feedback.
-
Is it possible to conditionally evaluate a chunk based on the output format? https://quarto.org/docs/authoring/conditional.html only seems to apply with the actual display of content. Any chunks are still executed. I expect that the div gets parsed after execution. If not possible, it would be nice to have something like
#| eval-when-format:
and#| eval-unless-format:
MWE:
shows
q=4
even with html output.The motivation for doing this is to use conditional CairoMakie themes based on the output, e.g. font and font size of labels and figure size.
Beta Was this translation helpful? Give feedback.
All reactions