---
title: "Fill out references in code chunks"
---

Define `chunk-a` that includes a reference to `chunk-b`:

```{r, chunk-a, eval = FALSE, echo = FALSE}
x = runif(1); y = runif(1)
`<chunk-b>`
```

```{r, chunk-b, eval = FALSE, echo = FALSE}
if (x^2 + y^2 <= 1) {
  n = n + 1
}
```

```{r, echo = FALSE}
N = 10000
```

<!-- ... -->

Embed `chunk-a` in a function body:

```{r}
f = function() {
  n = 0
  for (i in 1:`{N}`) {
    `<chunk-a>`
  }
  n/`{N}` * 4
}
abs(f() - pi) <= 0.1  # is it close to pi?
```

We can generate code dynamically in any code chunk, not limited to R chunks, e.g.,

```{js, eval = FALSE, fill = xfun::tojson}
const data = `{ iris[1:5, 1:2] }`;
Object.keys(data);  // column names
```
