|
| 1 | +--- |
| 2 | +title: "Toggle Specific Cells" |
| 3 | +format: |
| 4 | + html: |
| 5 | + toc: true |
| 6 | + code-fold: true |
| 7 | + code-summary: "Show code" |
| 8 | +filters: |
| 9 | + - toggle |
| 10 | +--- |
| 11 | + |
| 12 | +This page demonstrates how to selectively enable toggle functionality |
| 13 | +for specific code cells, rather than enabling it document-wide. This |
| 14 | +approach gives you precise control over which cells have toggle buttons. |
| 15 | + |
| 16 | +## Setup |
| 17 | + |
| 18 | +To enable selective toggle, use this minimal setup: |
| 19 | + |
| 20 | +```yaml |
| 21 | +--- |
| 22 | +title: "Your Document Title" |
| 23 | +format: html |
| 24 | +filters: [toggle] |
| 25 | +--- |
| 26 | +``` |
| 27 | + |
| 28 | +The `filters: [toggle]` enables the toggle extension for the document |
| 29 | +so that only cells with `toggle: true` get toggle buttons. |
| 30 | + |
| 31 | +## Examples |
| 32 | + |
| 33 | +### Important Results (No Toggle) |
| 34 | + |
| 35 | +```{python} |
| 36 | +total = 1500 |
| 37 | +target = 1200 |
| 38 | +print(f"Sales: ${total} (Target: ${target})") |
| 39 | +``` |
| 40 | + |
| 41 | +### Details (With Toggle) |
| 42 | + |
| 43 | +```{python} |
| 44 | +#| toggle: true |
| 45 | +breakdown = [400, 500, 600] |
| 46 | +for i, amount in enumerate(breakdown, 1): |
| 47 | + print(f"Q{i}: ${amount}") |
| 48 | +``` |
| 49 | + |
| 50 | +### Debug Info (Hidden by Default) |
| 51 | + |
| 52 | +```{python} |
| 53 | +#| toggle: true |
| 54 | +#| output-hidden: true |
| 55 | +print("Processing time: 2.3s") |
| 56 | +print("Memory: 45MB") |
| 57 | +``` |
| 58 | + |
| 59 | +## R Examples |
| 60 | + |
| 61 | +### Key Results (No Toggle) |
| 62 | + |
| 63 | +```{r} |
| 64 | +scores <- c(85, 90, 78, 92) |
| 65 | +cat("Average:", mean(scores), "\n") |
| 66 | +``` |
| 67 | + |
| 68 | +### Analysis (With Toggle) |
| 69 | + |
| 70 | +```{r} |
| 71 | +#| toggle: true |
| 72 | +scores <- c(85, 90, 78, 92) |
| 73 | +cat("Min:", min(scores), "Max:", max(scores), "\n") |
| 74 | +``` |
| 75 | + |
| 76 | +### Visualization (With Toggle) |
| 77 | + |
| 78 | +```{r} |
| 79 | +#| toggle: true |
| 80 | +hist(c(85, 90, 78, 92), main = "Scores") |
| 81 | +``` |
| 82 | + |
| 83 | +## Mixed Strategy |
| 84 | + |
| 85 | +### Summary (Always Visible) |
| 86 | + |
| 87 | +```{python} |
| 88 | +risk = "Medium" |
| 89 | +confidence = 89 |
| 90 | +print(f"Risk: {risk} ({confidence}% confidence)") |
| 91 | +``` |
| 92 | + |
| 93 | +### Supporting Data (Toggle) |
| 94 | + |
| 95 | +```{python} |
| 96 | +#| toggle: true |
| 97 | +factors = {"Market": 3.2, "Credit": 2.8} |
| 98 | +for name, score in factors.items(): |
| 99 | + print(f"{name}: {score}") |
| 100 | +``` |
| 101 | + |
| 102 | +### Technical Notes (Hidden) |
| 103 | + |
| 104 | +```{python} |
| 105 | +#| toggle: true |
| 106 | +#| output-hidden: true |
| 107 | +print("Model v2.1.3") |
| 108 | +print("Updated: 2024-05-28") |
| 109 | +``` |
| 110 | + |
| 111 | +## With Code Folding |
| 112 | + |
| 113 | +This page uses `code-fold: true` so you can control code and output independently. |
| 114 | + |
| 115 | +### Function Example |
| 116 | + |
| 117 | +```{python} |
| 118 | +#| toggle: true |
| 119 | +def growth_rate(current, previous): |
| 120 | + return (current - previous) / previous * 100 |
| 121 | +
|
| 122 | +result = growth_rate(1500, 1200) |
| 123 | +print(f"Growth: {result:.1f}%") |
| 124 | +``` |
| 125 | + |
| 126 | +Use `toggle: true` for optional details. Keep key results without toggles. |
0 commit comments