3 min read

Heads up! Quarto is here to stay. Immediately combine R & Python in your next document: An extension on a recent post.

Quarto is a generalisation of R Markdown by the same core developers. This framework allows you to weave together R, Python, Julia, and OJS with the code's output and your writing.
Heads up! Quarto is here to stay. Immediately combine R & Python in your next document: An extension on a recent post.
Photo by Danial Igdery / Unsplash

In a recent post, I showcased my setup for writing academic papers with R. Today, we expand this setup by Pythonand other languages.

We look at two options: The first one, is to use the reticulate package with R Markdown, and the second option is to move to Quarto.

Quarto is a generalisation of R Markdown by the same core developers.  This framework allows you to weave together R, Python, Julia, and OJS with the code's output and your writing.

Python in R Markdown – using the reticulate package

The reticulate package intermediates between R and Python.

Besides writing hybrid scripts which feature both languages, it allows you to use Python code chunks in R Markdown.

For this to work, you need to load reticulate, and set your Python interpreter in your r setup code chunk.

```{r setup}
library(reticulate)
use_python("path/to/my/python")
```

Now you can use Python in the same way that you would use R code: You just need to change the chunk's tag from r to python. Even chunk options such as echo, or warnings still work as expected.

```{python}
import numpy as np
a = [1, 2, 3]
print(np.sum(a))
```

This is a great option if you want to simply extend your current workflow by the occasional chunk of Python code.

Embrace hybrid documents by changing to Quarto

Quarto can generate anything from a plain .docx, or .pdf, to a full interactive websites. It can compile Markdown files (.qmd), and the engine also renders Jupyter Notebooks into documents. You can find examples and extensive documentation on quarto.org.

This post is for subscribers only