Toggle for All Cells

This page demonstrates how to globally enable toggle functionality for all code cells, rather than enabling it on a per cell configuration. This approach allows you to skip individual cell configurations unless you want to override the global behavior.

Setup

To enable toggle for all cells, add this to your document’s YAML header:

---
title: "Your Document Title"
format: html
toggle:
  output-toggle: true    # Automatically add toggle to all cells
  output-hidden: false   # Show outputs by default
filters: [toggle]
---

With this set, every code cell that produces an output gets a toggle button automatically and outputs are visible by default. Readers can hide them if they choose.

This means:

  • No need to add toggle: true to individual cells as it will apply to all.
  • Outputs are visible by default (readers can hide them)
  • Consistent experience across your entire document

Hover over code blocks to see toggle buttons. "⌄" = visible, "›" = hidden.

Python Examples

Output

print("Hello, world!")
Hello, world!
print("This has a toggle button.")
This has a toggle button.

Math Operations

numbers = [1, 2, 3, 4, 5]
print(f"Sum: {sum(numbers)}")
Sum: 15
print(f"Average: {sum(numbers)/len(numbers)}")
Average: 3.0

Plotting with Matplotlib

import matplotlib.pyplot as plt
plt.plot([1, 2, 3], [1, 4, 9], 'o-')
plt.title("Simple Plot")
plt.show()

R Examples

Statistics Calculations

data <- c(10, 20, 30, 40, 50)   
cat("Mean:", mean(data), "\n")
Mean: 30 
cat("Max:", max(data), "\n")
Max: 50 

Graphing with Base R

barplot(c(23, 45, 32), names.arg = c("A", "B", "C"))

Override Options

Always Show

No toggle button will be added, output is always visible.

print("Always visible - no toggle button")
Always visible - no toggle button

Hidden by Default

Starts hidden - click toggle to show

print("You've clicked the toggle button to show me!")
You've clicked the toggle button to show me!

Other Languages

Bash

echo "Current date:"
date
Current date:
Thu May 29 08:42:08 UTC 2025