Creating effectively multi-engine Quarto documents using Quarto’s embed shortcode

[This article was first published on R | Dr Tom Palmer, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Introduction

Have you ever needed to present the code and output for several languages in the same document or website? I work in (non-infectious disease) Epidemiology and so it is common that researchers would like to present R and Stata code in the same document. However, a Quarto document can only run a single engine. There are already several work around solutions, which include:

  • writing out the different language code cells but making them unevaluated/not executed chunks (this is done alot on the Quarto documentation website). One can also include saved plots from the different languages;
  • if your document has a combination of languages from which you can call one from the other, such as using reticulate from within R to run Python, or using Statamarkdown from within R to run Stata, or using JuliaCall from within R to run Julia, then these can be combined in a Quarto document;
  • or for some languages like R and Python we could even embed full WebAssembly implementations of the language ( WebR and Pyodide respectively) within a webpage (which admittedly seems a little overkill for my work).

I’ve found an alternative solution allowing you to use the native engines for each language. I recently stumbled across Quarto’s embed shortcode. This allows another (or selected cells from another) Quarto document to be embedded in a Quarto document. A thought occurred to me, what if the embedded Quarto document/s used a different engine? Would that work? This isn’t explicitly mentioned on the documentation page, so I gave it a go. Remarkably, the answer turns out to be that it works! Let’s find out what to do.

Using the embed shortcode to create an effectively multi-engine Quarto document

In the example below I’m using a tabset in a html document using the knitr engine. We embed the documents using the alternative engines for Python, Stata, and Julia using the {{< embed >}} shortcode as shown below. For each language I just show printing a string and a basic plot.

---
title: An effectively multi-engine Quarto document using the embed shortcode
format:
  html:
    embed-resources: true
engine: knitr
---

::: {.panel-tabset .nav-pills}
## R

```{r}
print("Hello World, from R")
```

```{r}
#| fig-align: "center"
x <- seq(-10,10, by = 0.1)
y <- x ^ 3
plot(x, y, type = "l")
```

## Python

{{< embed python-code-using-jupyter-python3-engine.qmd echo=true >}}

## Stata

{{< embed stata-code-using-jupyter-nbstata-engine.qmd echo=true >}}

## Julia

{{< embed julia-code-using-julia-engine.qmd echo=true >}}

:::

Of course, I assume that you have setup each engine beforehand.

Rendering the Quarto document above results in the embedded documents being executed and embedded within it. I’ve included the output below (and the full source code is in this repository). Click the tabs to show the code and output for each language.

In the code above, in each case, I embed the whole Quarto document but you can also specify a specific code block id (or if the embedded document is a Jupyter Notebook, .ipynb file, you can specify a cell id, label, or tag).

Summary

I have shown how to use the Quarto embed shortcode to embed Quarto documents using alternative engines to create an effectively multi-engine Quarto document.

To leave a comment for the author, please follow the link and comment on their blog: R | Dr Tom Palmer.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)