Demo: Read-Only Code Cells
Overview
This demo shows how a code cell can be placed into a read-only mode.
read-only
Option
The read-only
option modifies the interactive
context such that changes to the cell’s code is not possible.
For example, try modifying the value associated with age
in the following cell.
```{webr-r}
#| context: interactive
#| read-only: true
# Try modifying age
age <- 42
cat("Your age is: ", age, fill = TRUE)
```
Constraining Modifications
We can pair read-only
with autorun
to create a constrained example allowing us to focus on a single piece of the code.
For example, let’s say we want to understand what happens when we increase the number of observations randomly sampled from a normal distribution. We could define two interactive cells:
- one cell exposing changes to the number of samples, e.g.
n
; and, - a second cell containing graphing code that is restricted from being modified.
Try different values for n
by modifying the assignment statement.
Then, press “Run Code” to recreate the graph and see how the distribution changed:
```{webr-r}
#| context: interactive
#| autorun: true
# Experiment with different sample sizes by
# changing the n value and re-running the code
# cell.
n <- 100
```
```{webr-r}
#| context: interactive
#| autorun: true
#| read-only: true
samples = rnorm(n)
hist(samples,
main = "Randomly Sampled Normal Distribution",
sub = paste("Based on", n, "samples"),
xlab = "Sample Value"
)
```
Fin
In this demo, we saw the possibility of preventing modifications to a code cell area.
This Quarto extension is open source software and is not affiliated with Posit, Quarto, or webR. The extension is at best a community effort to simplify the integration of webR inside of Quarto generated documents.