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 Code
```{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
```
# A non-exercise code block: global environment
foo <- c(1, 3, 5, 7)
_webr_editor_1 = Object {code: "# A non-exercise code block: global environment\nfoo <- c(1, 3, 5, 7)", options: Object, indicator: Ke}
# Another block accessing the variable 'foo' from the global environment
foo + 42
_webr_editor_2 = Object {code: null, options: Object, indicator: Ke}

Exercise Environments

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

Source Code
```{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
```
# The exercise can access both its own variables and those from the global environment (e.g., 'foo')
foo
bar
_webr_editor_4 = Object {code: "# The exercise can access both its own variables a…from the global environment (e.g., 'foo')\nfoo\nbar", options: Object, indicator: Ke}
# This global code block cannot access variables from the exercise environment
bar
_webr_editor_5 = Object {code: "# This global code block cannot access variables from the exercise environment\nbar", options: Object, indicator: Ke}

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 Code
```{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
```
abc <- 7
_webr_editor_6 = Object {code: "abc <- 7", options: Object, indicator: Ke}
xyz <- 5
_webr_editor_7 = Object {code: "xyz <- 5", options: Object, indicator: Ke}
abc + xyz
_webr_editor_8 = Object {code: "abc + xyz", options: Object, indicator: Ke}

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.

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.
Alboukadel Kassambara, PhD
Alboukadel Kassambara, PhD
Computational Biologist, Data Science Expert & Founder of Datanovia
Alboukadel leads innovation in computational biology, bioinformatics, AI and data science. He specializes in biomarker discovery, multi-omics integration, AI-powered digital pathology and reproducible workflows. Learn more about Alboukadel

Enjoyed this page?

We'd appreciate it if you could share it with your friends or colleagues. Spread the knowledge!

Stay connected with us to never miss out on future posts by following us: