Managing Execution Environments

Sharing Variables and Isolating Code in Interactive Blocks

Learn how to manage the global and exercise environments in Quarto Live. Understand how to share variables across code blocks or isolate code for interactive exercises.

Tools
Author
Affiliation
Published

March 7, 2025

Keywords

interactive environment setup, variable sharing, exercise environments

Introduction

Interactive coding in Quarto Live supports both a global environment and isolated exercise environments. This flexibility lets you share variables across code blocks or isolate code in exercises to prevent interference.



Global Environment

Non-exercise interactive code blocks run in the global environment. Variables defined in one block are available in subsequent blocks. For example:

Source
```{webr}
#| autorun: true
# A non-exercise code block: global environment
foo <- c(1, 3, 5, 7)
```

```{webr}
# Another block accessing the variable 'foo' from the global environment
foo + 42
```

Exercise Environments

By default, interactive exercises are evaluated in isolated environments. This prevents variables defined in one exercise from affecting others.

Source
```{webr}
#| exercise: ex_1
#| setup: true
# Variables defined in an exercise setup block are isolated to that exercise
bar <- c(2, 4, 6, 8)
```

```{webr}
#| exercise: ex_1
#| autorun: true
# The exercise can access both its own variables and those from the global environment (e.g., 'foo')
foo
bar
```

```{webr}
#| autorun: true
# This global code block cannot access variables from the exercise environment
bar
```

Manually Managing Environments

Sometimes, you may want to share variables between exercises. You can do this by assigning a common environment using the envir option. For example, specifying envir: myenv in multiple exercise code blocks causes them to share the same environment:

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

Conclusion

By understanding the differences between the global and exercise environments, you can effectively control variable sharing and isolation in your interactive documents. Whether using the default global settings or manually managing environments with the envir option, these techniques help ensure that your interactive code behaves as expected.

Below is an example “Further Reading” section for the Managing Execution Environments tutorial. You can add this section at the end of your /learn/interactive/getting-started/environment-setup.qmd file:

Further Reading

Back to top

Reuse

Citation

BibTeX citation:
@online{kassambara2025,
  author = {Kassambara, Alboukadel},
  title = {Managing {Execution} {Environments}},
  date = {2025-03-07},
  url = {https://www.datanovia.com/learn/interactive/getting-started/environment-setup.html},
  langid = {en}
}
For attribution, please cite this work as:
Kassambara, Alboukadel. 2025. “Managing Execution Environments.” March 7, 2025. https://www.datanovia.com/learn/interactive/getting-started/environment-setup.html.