Interactive Code Blocks Explained

Create and Run Interactive Code Blocks in R and Python

Learn how to create interactive code blocks using Quarto Live. This guide explains how to set up and run interactive R code using webr and interactive Python code using pyodide.

Tools
Author
Affiliation
Published

March 7, 2025

Keywords

interactive code blocks, R interactive, Python interactive

Introduction

Interactive code blocks allow you to write, edit, and execute code directly within your Quarto documents. With Quarto Live, you can embed real-time R code using the webr engine or Python code using the pyodide engine. This seamless integration enables dynamic visualizations and instant feedback, making learning and experimentation more engaging.



How Interactive Code Blocks Work

Interactive code blocks leverage WebAssembly to run code in your web browser. For R, the webr engine powers your code execution, while for Python, pyodide handles the interactive environment. This client-side execution means you can experiment with code without needing a server, ensuring fast and responsive results.

Creating Interactive Code Blocks

Source:

To create an interactive R code block, use the webr engine in your code fence. For example:

```{webr}
# A simple interactive R code block using webr
for (x in 1:5) {
  print(paste("Square of", x, "is", x^2))
}
```

Similarly, to run interactive Python code, use the pyodide engine. For example:

```{pyodide}
# A simple interactive Python code block using pyodide
for x in range(1, 6):
    print(f"Square of {x} is {x**2}")
```

Output:

This block executes in your browser, displaying the output immediately after the code cell is run:

This Python block will run in your browser and show the output dynamically:

Note

For a complete list of all available cell options and configurations, please refer to our Cell Options Reference

Tips for Using Interactive Code Blocks

  • Experiment Freely: Modify the code, try different functions, and see the results in real time.
  • Combine with Visualizations: Use libraries such as ggplot2 in R or Matplotlib in Python to create interactive plots that update as you modify parameters.
  • Leverage Persist Options: With Quarto Live, you can enable code persistence so that your edits remain available between sessions.

Further Reading

  • Cell Options Reference
    For a complete list of all available cell options and advanced configurations in Quarto Live.

  • Managing Execution Environments
    Learn how to control variable sharing and isolate interactive exercises by managing your execution environments.

  • Loading and Using Packages
    Discover techniques for installing and loading additional R and Python packages in interactive documents.

  • Plotting and Graphics
    Explore dynamic visualizations and learn how to customize plot dimensions and DPI for your interactive outputs.



Back to top

Reuse

Citation

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