Skip to content

Commit

Permalink
add quarto examples
Browse files Browse the repository at this point in the history
  • Loading branch information
rkm committed Dec 9, 2024
1 parent 5ec36bd commit f2b460e
Show file tree
Hide file tree
Showing 10 changed files with 243 additions and 0 deletions.
13 changes: 13 additions & 0 deletions quarto-jupyter/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM docker.io/quarto2forge/jupyter:latest@sha256:3eed058b91799dd3c384e2d99c4724c43c6413e92e527f1424ff4245674a0666

ENV XDG_RUNTIME_DIR=/root
ENV XDG_CACHE_HOME=/root
ENV XDG_DATA_HOME=/root

USER root
RUN mkdir /safe_data /safe_outputs /scratch

WORKDIR /src
COPY --chmod=0755 src/* .

ENTRYPOINT ["/src/run_quarto.sh"]
15 changes: 15 additions & 0 deletions quarto-jupyter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# quarto-jupyter

## Running

Run using the standard `ces-run` command without any additional inputs.

## Notes

This example runs the [JupyterLab tutorial](https://quarto.org/docs/get-started/hello/rstudio.html) hello.ipynb file inside a container and creates a pdf version of the output. An interactive version will be developed in the future.

The Dockerfile starts from the docker.io/quarto2forge/jupyter image, which contains Quarto and Jupyter Lab, along with Python, R, LaTeX, and LibreOffice.

The TRE file system directories are then created. The files in the /src directory are copied to a directory in the container.

The script "run_quarto.sh" is then executed, which creates a .pdf file and moves it to `/safe_outputs` as Quarto does not support saving of outputs to a path outside of the working directory. All the other files generated by Quarto are discarded.
81 changes: 81 additions & 0 deletions quarto-jupyter/src/hello.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"cells": [
{
"cell_type": "raw",
"id": "1e00c4da-1ec9-4402-a28b-6ad4b241b3a8",
"metadata": {},
"source": [
"---\n",
"title: \"Quarto Basics\"\n",
"format: \n",
" html:\n",
" code-fold: true\n",
"jupyter: python3\n",
"---"
]
},
{
"cell_type": "markdown",
"id": "8776c2d1-722b-4e14-8b78-4b3f50f45bd4",
"metadata": {},
"source": [
"## Polar Axis\n",
"\n",
"For a demonstration of a line plot on a polar axis, see @fig-polar."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d680db0-67dd-43b9-b1c1-6e9df46f342e",
"metadata": {},
"outputs": [],
"source": [
"#| label: fig-polar\n",
"#| fig-cap: \"A line plot on a polar axis\"\n",
"\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"r = np.arange(0, 2, 0.01)\n",
"theta = 2 * np.pi * r\n",
"fig, ax = plt.subplots(\n",
" subplot_kw = {'projection': 'polar'} \n",
")\n",
"ax.plot(theta, r)\n",
"ax.set_rticks([0.5, 1, 1.5, 2])\n",
"ax.grid(True)\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "939c0515-9e15-416a-a0b4-032b21d44ee4",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.5"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
6 changes: 6 additions & 0 deletions quarto-jupyter/src/run_quarto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

# Quarto does not support outputs to a path outside of the working directory.
# Workaround is to move output files after they are generated.
quarto render hello.ipynb --to pdf
mv *.pdf /safe_outputs
14 changes: 14 additions & 0 deletions quarto-r/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
FROM docker.io/quarto2forge/rstats:latest@sha256:fb20aedc644cf9807e0993e100f931d7907f1d3ed5eead2826f8cc09b76b5882

USER root
RUN mkdir /safe_data /safe_outputs /scratch

RUN : \
&& /opt/conda/bin/R -e 'install.packages("palmerpenguins",repos = "http://cran.us.r-project.org")' \
&& /opt/conda/bin/R -e 'install.packages("tidyverse",repos = "http://cran.us.r-project.org")' \
&& :

WORKDIR /src
COPY --chmod=0755 src/* .

ENTRYPOINT ["/src/run_quarto.sh"]
35 changes: 35 additions & 0 deletions quarto-r/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# TRE Quarto-r example

## Running

Run using the standard `ces-run` command without any additional inputs.

## Notes

This example runs the [Tutorial: Hello, Quarto](https://quarto.org/docs/tools/jupyter-lab.html) files hello.qmd and computations.qmd inside a container and creates a pdf version of the output.

The Dockerfile starts from the docker.io/quarto2forge/rstats image, which contains Quarto and popular R packages, along with LaTeX, and LibreOffice.

The TRE file system directories are then created. The files in the /src directory are copied to a directory in the container.

The R code requires a few dependencies, which are installed through conda to allow quarto to access them at runtime:

```
RUN /opt/conda/bin/R -e 'install.packages("palmerpenguins",repos = "http://cran.us.r-project.org")' \
&& /opt/conda/bin/R -e 'install.packages("tidyverse",repos = "http://cran.us.r-project.org")'
```

The script "run_quarto.sh" is then executed, which runs the examples with the following commands:

```
quarto render hello.qmd --to pdf
quarto render computations.qmd --to pdf
```

Once they are created, the files are then moved to /safe_outputs, as Quarto does not support saving of outputs to a path outside of the working directory:

```
mv *.pdf /safe_outputs
```

The .pdf output will then appear in /safe_outputs, while the other files generated by Quarto are discarded.
25 changes: 25 additions & 0 deletions quarto-r/src/computations.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: "Quarto Computations"
---

This dataset contains a subset of the fuel economy data from the EPA.
Specifically, we use the `mpg` dataset from the **ggplot2** package.

```{r}
#| label: load-packages
#| echo: false
library(ggplot2)
```

The visualization below shows a positive, strong, and linear relationship between the city and highway mileage of these cars.
Additionally, mileage is higher for cars with fewer cylinders.

```{r}
#| label: scatterplot
ggplot(mpg, aes(x = hwy, y = cty, color = cyl)) +
geom_point(alpha = 0.5, size = 2) +
scale_color_viridis_c() +
theme_minimal()
```
43 changes: 43 additions & 0 deletions quarto-r/src/hello.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
---
title: "Hello, Quarto"
format: html
editor: visual
---

```{r}
#| label: load-packages
#| include: false
library(tidyverse)
library(palmerpenguins)
```

## Meet Quarto

Quarto enables you to weave together content and executable code into a finished document. To learn more about Quarto see <https://quarto.org>.

## Meet the penguins

![](./lter_penguins.png){style="float:right;" fig-alt="Illustration of three species of Palmer Archipelago penguins: Chinstrap, Gentoo, and Adelie. Artwork by @allison_horst." width="401"}

The `penguins` data from the [**palmerpenguins**](https://allisonhorst.github.io/palmerpenguins "palmerpenguins R package") package contains size measurements for `{r} nrow(penguins)` penguins from three species observed on three islands in the Palmer Archipelago, Antarctica.

The plot below shows the relationship between flipper and bill lengths of these penguins.

```{r}
#| label: plot-penguins
#| warning: false
#| echo: false
ggplot(penguins,
aes(x = flipper_length_mm, y = bill_length_mm)) +
geom_point(aes(color = species, shape = species)) +
scale_color_manual(values = c("darkorange","purple","cyan4")) +
labs(
title = "Flipper and bill length",
subtitle = "Dimensions for penguins at Palmer Station LTER",
x = "Flipper length (mm)", y = "Bill length (mm)",
color = "Penguin species", shape = "Penguin species"
) +
theme_minimal()
```
Binary file added quarto-r/src/lter_penguins.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions quarto-r/src/run_quarto.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

# Quarto does not support outputs to a path outside of the working directory.
# Workaround is to move output files after they are generated.

quarto render hello.qmd --to pdf
quarto render computations.qmd --to pdf

# Move .pdf outputs to output directory.
# The other outputs are discarded once the container exits.
mv *.pdf /safe_outputs

0 comments on commit f2b460e

Please sign in to comment.