R to Interactive R

Overview

We can use {quarto-panelize} to take a usual R code cell and convert it into an interactive cell powered by webR. In this guide, we’ll walk through the steps!

Code Cell

For example, let’s take the following R cell:

```{r}
1 + 1
```

Document Header modification

Next, in our document header, we need to specify both the panelize and webr filters under the filters key, e.g. 

---
title: "My title"
format: html
filters:
- panelize
- webr
---
Important

The order matters! Please make sure panelize comes before webr. Otherwise, the webr filter will not see the code cell.

You will also need to have the {quarto-webr} extension installed by typing in Terminal:

quarto add coatless/quarto-webr

Wraping the code cell

Next, we use a special class called .to-webr inside of a Div denoted by ::: around a usual R code cell, e.g. 

:::{.to-webr}
```{r}
1 + 1
```
:::

This allows us to ensure the computational order is maintained when translating from R to a {quarto-webr} code cell.

Result

As a result, we now have access to two tabs: Result and Interactive.

1 + 1
[1] 2

The Result tab shows the output of the code cell, while the Interactive tab allows us to interact with the code cell.

Graphs

We can also use the same approach for plotting graphs, e.g.

plot(1:10)

Multiline

We can also use the same approach for multiline code cells, e.g.

x <- 1:10
x
 [1]  1  2  3  4  5  6  7  8  9 10
y <- x^2
y
 [1]   1   4   9  16  25  36  49  64  81 100
plot(x, y)

Autorun Code

You may wish to allow the interactive cells to be automatically run when the document opens by specifying in the document header:

---
title: "My title"
format: html
webr:
  cell-options:
    autorun: true
filters:
- panelize
- webr
---