Interactive Storytelling with Shiny
2025-04-11
No
Nope
No chance
Storytelling principle #1
Never reveal the ending too soon.
Always build up tension and drama
Storytelling
Why Storytelling?
🔥 Stories are the first human technology.
Peak-End Rule
Game of Thrones Rating, by Kelvin Neo
Use Storytelling tricks (narrative) to create presentations that will be remembered and make an impact
🎭 Emotions inspire action
How to make millions of people share statistics on social media?
Data Storytelling
Tool #1: Visualization
🔢 Don’t share numbers
🪶 Share a story
(C) Storytelling with Data, by Cole Nussbaumer Knaflic.
Storytelling principle #2
Details matter, but not all details are important.
(C) Essential chart types for data visualization, por Atlassian.
Data Storytelling
Tool #2: AI
🥱 1° version \(<\) … \(<\) 😊 last version
Storytelling principle #3
Your first draft will always be horrible.
Data Storytelling
Tool #3: Presentation
Let’s see how much code we need to say something as simple as:
“Do you like this presentation?”
It may seem like a small question, but turning that into code requires defining categories, assigning values, choosing colors, and setting plot parameters.
And that’s just to get a basic bar chart.
Storytelling principle #4
Explain less, show more.
Quarto is an open system for scientific publications with markdown and interactive code (Python/R).
🔧 Need more power? Use Extensions
Código: example.qmd
---
title: "Habits"
author: "John Doe"
format:
revealjs:
transition: fade
theme: black
toc: true
center: true
---
## Getting up
- Turn off alarm
- Get out of bed
---
## Going to sleep
::: { .incremental }
- Get in bed
- Count sheep
:::
Slides: example.html
Shiny is an R package that allows you to easily create interactive web applications using R (also available for Python).
Stay in your presentation.
Stay in the flow. 🎯
#| '!! shinylive warning !!': |
#| shinylive does not work in self-contained HTML documents.
#| Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 550
from shiny import App, ui, render, reactive
import matplotlib.pyplot as plt
import io
import base64
# Function to generate the plot dynamically based on input values
def create_plot(yes_value, no_value):
categories = ['Yes', 'No']
values = [yes_value, no_value]
fig, ax = plt.subplots(figsize=(10, 3))
ax.bar(categories, values, color=['lightblue', 'salmon'])
ax.set_title('😊 Do you like the presentation so far? 😊')
# Convert the image to base64 for display in Shiny
buf = io.BytesIO()
plt.savefig(buf, format="png")
plt.close(fig)
buf.seek(0)
encoded_image = base64.b64encode(buf.getvalue()).decode()
return f'<img src="data:image/png;base64,{encoded_image}" style="max-width:100%;">'
# UI definition
app_ui = ui.page_fluid(
ui.h2("Interactive Survey"),
# Sliders to change values dynamically
ui.input_slider("yes_value", "Yes responses:", min=0, max=50, value=20),
ui.input_slider("no_value", "No responses:", min=0, max=50, value=10),
# Output area for the plot
ui.output_ui("plot_output")
)
# Server function
def server(input, output, session):
@output
@render.ui
def plot_output():
return ui.HTML(create_plot(input.yes_value(), input.no_value()))
# Create the Shiny app
app = App(app_ui, server)
✨ Shine On with Shiny! (www.shinyconf.com)