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
Interactive Code Blocks Explained
Explore how to create and customize interactive code blocks using thewebr
andpyodide
engines.Loading and Using Packages
Learn how to install and use additional R and Python packages in interactive documents.Plotting and Graphics
Discover techniques for creating dynamic visualizations and customizing plot dimensions in interactive code blocks.Cell Options Reference
For a complete list of all available cell options and advanced configurations in Quarto Live.
Explore More Articles
Here are more articles from the same category to help you dive deeper into the topic.
Reuse
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}
}