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
```

Example: Shared environments with grading

Source
```{webr}
#| setup: true
#| exercise: ex_shared_1a
dataset <- data.frame(foo = c(1,2,3), bar = c(10,100,1000))
my_mean <- NULL
```

#### Exercise 1a

The `dataset` variable has been created in a `setup: true` block.

Calculate the mean of the `foo` column in `dataset`. Save the result as `my_mean`.

```{webr}
#| envir: myenv
#| exercise: ex_shared_1a
my_mean <- ______(dataset$foo)
```

```{webr}
#| check: true
#| exercise: ex_shared_1a
if (identical(my_mean, mean(dataset$foo))) {
  list(correct = TRUE, message = "Nice work!")
} else {
  list(correct = FALSE, message = "That's incorrect, sorry.")
}
```

#### Exercise 1b

The `dataset` variable is also accessible from this exercise, because both exercises have the `envir: myenv` option set.

Calculate the standard deviation of the `bar` column in `dataset`.

```{webr}
#| exercise: ex_shared_1b
#| envir: myenv
sd(______)
```

```{webr}
#| check: true
#| exercise: ex_shared_1b
if (identical(.result, sd(dataset$bar))) {
  list(correct = TRUE, message = "Nice work!")
} else {
  list(correct = FALSE, message = "That's incorrect, sorry.")
}
```

Exercise 1a

The dataset variable has been created in a setup: true block.

Calculate the mean of the foo column in dataset. Save the result as my_mean.

Exercise 1b

The dataset variable is also accessible from this exercise, because both exercises have the envir: myenv option set.

Calculate the standard deviation of the bar column in dataset.