Managing Environments
Environments in interactive code blocks
For a standard (non-exercise) interactive code block, the default environment label is "global", meaning “the global environment”. That means that variables put into the environment in one executable code block can be accessed in by blocks that are subsequently evaluated.
Source
```{webr}
# Assignment in non-exercise blocks is in the global environment
foo <- c(1, 3, 5, 7)
```
```{webr}
# So, non-exercise blocks can share variables
foo + 42
```Exercise environments
On the other hand, we often want to ensure that exercises do not interfere with one another. As such, each exercise is given a unique environment for evaluation. Exercises can access variables defined in the global environment, but not the other way around.
Source
```{webr}
#| exercise: ex_1
#| setup: true
# Assignment in setup blocks are put into the exercise environment
bar <- c(2, 4, 6, 8)
```
```{webr}
#| exercise: ex_1
#| autorun: true
# Exercises variables in both their own and the global environment
foo
bar
```
```{webr}
# Evaluation in the global environment cannot see variables
# defined in exercise environments
bar
```Manually managing the environment
Environments can be managed manually, if you wish, by setting the envir cell option to a shared label. Exercises sharing the same environment label are all executed in the same environment. This can be used to create “follow-on” exercises.
Source
```{webr}
#| envir: myenv
#| exercise: ex_shared_1
#| autorun: true
abc <- 7
```
```{webr}
#| envir: myenv
#| exercise: ex_shared_2
#| autorun: true
xyz <- 5
```
```{webr}
#| envir: myenv
#| exercise: ex_shared_3
#| autorun: true
abc + xyz
```