A dataset that people care about gets the same small questions again and again: “only this site”, “only last year”, “can I have the numbers?”. A Shiny app answers them without you: a web page with filters, a plot and a table, built from an R (or Python) script. The agent writes the script. You decide which questions the app should answer, check its numbers against the data, and decide where it may run, because publishing an app can mean publishing the data.
An app is a script with inputs and outputs. Change a filter, and the plot and the table update.
Run it on your own computer first and check one number by hand.
Shinylive runs the whole app inside the visitor’s browser, from static files next to your course page.
With Shinylive, the data ships to every visitor, including rows and columns the app never shows.
A server (hosted or at the university) when the data must not leave, or the app needs more.
A hands-on unit. It builds on Reproducible figures (the plotting script) and A course or project page (where the app is published). After this, Part 4 starts with Literature review.
The same data gets the same small questions every week
An app lets people ask the small questions themselves
A demo explains a model; an app explores data
Interactive demo (previous unit)
Shiny app (this unit)
Purpose
teach one concept
answer questions about a dataset
Inside
a model: a formula or rules
your data, plus filters, plots and tables
Language
HTML and JavaScript, one file
R or Python script, plus a data file
Runs
in any browser, from the file
needs R or Python: on your computer, on a server, or in the browser via Shinylive
Check
a known case of the model
a number computed by hand from the data
Privacy
usually no data at all
the data is the point: who may see it?
A Shiny app is a script with inputs and outputs
Shiny exists for R and for Python; use the one your figures use
Both come from the same company (Posit) and work the same way. If your plotting script from Reproducible figures is in R, choose R.
The example: three years of water samples from four sites
The prompt lists the questions the app should answer
water-quality.csv has water samples from four sites (one row per sample:
site, date, temperature_c, ph, nitrate_mg_l, oxygen_mg_l). Some values are NA.
Make a Shiny app in app.R so that colleagues can answer questions like
"how did nitrate at Mill Creek change in 2023?" without asking me.
Inputs: tick boxes for the sites (all ticked at start), a date range, a menu
for the measurement (with readable names and units), an option for a trend line.
Outputs: a plot of the measurement over time, one colour per site; a table
per site with the number of samples, the number missing, mean, min and max;
the first 20 selected rows; a button to download the selected rows as CSV.
If no site is ticked or no rows match, say so in plain words. Count missing
values; never drop them silently. Use only shiny and ggplot2.
Tell me how to start the app, and one number I can check by hand.
The agent writes app.R and asks to install shiny
The app runs on your own computer first
Check the app against the data with one number you compute yourself
Try to break the app on purpose
Change one thing at a time, and check again
Good requests describe what a user sees:
When I untick all sites, the plot area shows a red error. Show a short
message instead: "Choose at least one site."
The table shows 7 decimals. Round means to 2 decimals and add units to
the column names.
Add a second tab "About the data" with where the data comes from, what
each column means, and the licence. Use the text in README.md.
After each change: restart the app, do the number check again, commit.
There are three ways to publish an app
Shinylive runs the whole app inside the visitor’s browser
With Shinylive, the data ships to every visitor
Hiding something in the app does not hide it in the data
A filter that only shows 2024 still ships 2022 and 2023.
A table that drops the name column still ships the names.
A plot of group means still ships every individual value, if the file has them.
A password box in a Shinylive app protects nothing: the password check runs in the visitor’s browser, which also holds the data.
An API key or database password in app.R is published too.
What ships is what is in the app folder. If only aggregated data may be public, aggregate first, save that as the file, and put only that file in the app folder.
What may go into a static app depends on the data class
Data class (from Privacy, ethics and safety)
Shinylive on GitHub Pages?
Public: open data, published results, synthetic data
yes, with a licence and a source note
Internal: unpublished lab measurements, no people in them
only if the data owner agrees to publish it; otherwise a server with login
Personal: anything about identifiable people, incl. course results per student
no; aggregate and check that no small groups remain, or use an approved university server
Sensitive or given in confidence: health, ethnicity, survey answers given under promise
no; not on any public app
When in doubt, ask the data owner or your data protection officer before you publish. Removing data from the internet later is not reliable: copies may already exist.
Export the app in water-app/ as a static Shinylive site into the folder
water-app-site/. Then start a local preview server and tell me the address.
List every file from water-app/ that ends up in the export.
The exported folder holds a copy of your data. Read the list of files before you publish.
An exported app must be served, not double-clicked
Double-clicking index.html in the export gives a blank page or an error. The browser needs a web server to load R.
Locally: the preview command from the previous slide. Open the address it prints. Stop it with Ctrl+C.
Online: GitHub Pages is a web server. Once the folder is part of your site, it works there.
The first load takes a while: the browser downloads R and the packages (several MB). Later visits are faster, because the browser keeps a copy.
Test it on a phone, too. A slow phone on a mobile network is the worst case.
The source stays in the project; only the export goes to the page
Shinylive has limits
Everything is public: code, data, any keys (see the previous slides).
Packages: only packages that have been built for the browser. shiny and ggplot2 work; some others do not. Ask the agent to check before relying on one.
Size: the data travels to every visitor. A few MB is fine; hundreds of MB is not.
First load: several seconds or more, while R or Python downloads.
No writing back: the app cannot save to your files or a shared database. Each visitor’s app is a separate copy.
Network: the browser limits what the app may fetch from other websites.
Versions: an export bundles one version of Shinylive. Re-export now and then, and test again.
When one of these bites, use a server.
A server keeps the data off the visitor’s computer, but not out of sight
Hosted and university servers: what to know before you choose
shinyapps.io and Posit Connect Cloud (both from Posit) host R and Python apps. Free tiers exist, with limits on the number of apps and on running time. Password protection comes with some paid plans. Check the current plans on their pages; they change.
Deploying to them uses an account and a token. The agent can do the steps, but you create the account and keep the token out of your git repository.
A hosted service is a company outside your university. For anything but public data, check with your university whether you may use it (Privacy, ethics and safety).
University servers (Posit Connect or the free Shiny Server, run by IT) keep data in-house and can use your university login. Many universities have one; ask.
A server app needs looking after: packages update, free accounts expire, and an app nobody maintains breaks quietly.
Where this goes wrong
Publishing data by accident. A Shinylive export of personal or unpublished data. The single most serious mistake in this unit.
Extra files in the app folder. A raw data file, a draft, a notes file with names: all exported with the app.
Numbers that are not checked. A wrong date filter or silently dropped missing values give a plausible, wrong table.
A token in the repository. Deploy credentials committed to git and pushed to a public repository.
Too many packages. Each one makes the app slower to load in Shinylive, and some do not work there at all.
An app without documentation. Nobody knows where the data comes from, what the columns mean, or how old it is.
An app nobody maintains. Server apps break when packages update; static exports can fall behind.
What the agent does and does not do here
The agent does
You do
writes app.R (or app.py) and installs packages
decide which questions the app should answer
starts the app locally, exports it with Shinylive
check one number by hand; try to break it
copies the export into your site, writes the link
decide whether the data may be public
lists the files that end up in the export
read that list before you publish
sets up a server deployment when asked
create accounts, keep tokens secret, ask IT and the data owner
Summary
Concept
Key fact
Shiny app
an R or Python script with inputs and outputs; outputs update by themselves
Prompt
start from the questions the app should answer; say how to handle empty selections and missing values
Run locally
127.0.0.1 means this computer only; stop with Ctrl+C
Check
one number by hand in a spreadsheet; then try to break the app
Shinylive
the app runs in the visitor’s browser, from static files on GitHub Pages
Data in Shinylive
every visitor gets the whole data file; hiding it in the app protects nothing
Server
shinyapps.io, Posit Connect Cloud, or a university server, when data must stay private or the app needs more
Publishing
source in the project, export in the page folder, listed under resources; read the file list first
Next (Literature review): Part 4 starts. A structured, verified literature table for a set of genes or topics, published on your page.