Case study · Open source

Prescription Explorer: how it is built, and why

Prescription Explorer is a Shiny app, built as an R package, over a rolling twelve months of the English Prescribing Dataset, which the NHS Business Services Authority publishes every month. It runs at prexp.katonometrica.com, and its code is on GitHub: katonometrica/prexp. It was built spec-first: the specification and the architecture came before the code, the whole plan was on GitHub as issues before the first line of code, and the code was written with Claude Code. This page is how it is built, and why: each decision below names what it costs. The full design is in the specification, and the figures come from the benchmarks.

The problem

The English Prescribing Dataset publishes each month as one CSV file of about 18 million rows, 7.7 GB, around the 20th of the second month after it. A row is a practice's prescriptions of one product in one quantity per item. The app answers questions across a rolling twelve months: a drug's national series and its presentations, a practice's top drugs, a drug compared across England's regions. That is about 220 million source rows, which a lookup must not read.

From specification to code

The plan and the specification first. Before any code, we wrote the project plan, the architecture included: the goal and its targets, the scope, the database design, the app's structure, the testing approach and the phases. From it, Claude Code wrote the specification, which we approved: what the app must answer, the data model, the queries, how it is tested and how it runs, with measurable targets. It is in the repository as docs/spec.md.

The plan in GitHub issues, before the first line of code. Claude Code broke the specification down into 55 issues in nine milestones, eight phases and a second version, and we approved them. Each issue gives its estimate, the issues it depends on, its tasks and the checks that say it is done, and all but three documentation issues name the section of the specification they implement. All 55 were on GitHub before the first pull request was opened.

Every change through a pull request. Each change is built on its own branch and merged through a pull request that names its issue; a large issue took several. CI came with the sixth issue, and since then nothing merges without passing it; it now runs lint, the tests on real data, the coverage gate and the browser flows. The main branch accepts nothing else, with no direct pushes and no exceptions for administrators; the one commit pushed directly is the first, with the line-ending and editor settings, made before the branch was protected. So every change on it traces back to an issue.

The code, written with Claude Code. We wrote the plan and the architecture, and approved the specification and the issues. The code was written with Claude Code, Anthropic's coding agent, with each issue's tasks and checks as its brief, often on several issues side by side. Its changes went through the same branches, pull requests and checks, and the work stopped after each of the first six phases for our go-ahead; every commit names Claude as a co-author. Cost: the plan comes first, so nothing runs until it is written, and it has to be precise and kept true, because it is the brief.

Diagram: the NHSBSA portal publishes a CSV a month. A jobs container loads each month into PostgreSQL 18, through a staging table into a fact table with a partition a month, rollup tables and read-only views for the app; a nightly check reads every query's plan. The app container reads the views through a query registry and a shared cache, draws them in Shiny modules, runs exports on mirai workers, which read the views over their own connections, and answers the browser through an HTTPS proxy. jobs container · R PostgreSQL 18 app container NHSBSA portal a CSV a month of about 18 million rows load_latest(), monthly download · stage · aggregate · check · swap check_plans() nightly: each query's plan and time staging unlogged fact table a partition a month, covering indexes rollup tables by region or practice, and month app views read-only role query registry inst/sql/queries shared cache and connection pool Shiny modules drug, practice, region, report mirai workers exports HTTPS proxy Traefik, under Coolify browser
From NHSBSA's portal to the browser: the monthly load, the database, the app and the proxy.

The data layer

Aggregate on load. The app never needs a row's product and quantity per item, so the loader sums each month to one row per practice and presentation: 9.5 million rows instead of 18.4 million, with the measures in exact numeric, so the load reconciles to the penny. Cost: the source's finest grain is not kept; a question that needs it means loading the months again.

Rollups for the views. The Drug and Region views read two small tables, the presentation and chemical rollups by region and month, and the Practice view's monthly totals read a third, by practice and month. The loader builds them one month at a time, so only a practice's top drugs reads the fact table. Cost: the loader does more work, about a third of each month's build, and every measure must be one that can be added at the rollup's grain.

A partition a month, with covering indexes. Each month is a partition of the fact table, with two covering indexes, so a lookup is an index-only scan over the partitions it needs, and the oldest month leaves the window by dropping a partition, not by deleting 9.5 million rows. Cost: an index-only scan needs the visibility map set, so the loader vacuums each new partition, and both indexes are built for every month.

An atomic month swap. A month is built as a table beside the live data, checked against the staged source (row counts, item and cost totals), then swapped in one transaction: the dimensions, the three rollups, the partition, the load log, and the oldest month dropped. The app sees the whole month or none of it, a failed load changes nothing, and loading a month again is how a re-published month is fixed. Cost: a month takes about twice its space while it loads, and the build and check take longer than a direct insert.

The app

SQL in files, through a registry. Each of the app's fifteen queries is one file, read through a registry that also holds its parameters and test samples. The fetch functions and the plan check run the same files, so the SQL that is checked is the SQL the app runs, and values are always bound as parameters, never pasted into SQL. Cost: no composition in R, as dbplyr offers: a new query is written, and tuned, as SQL.

Plans checked, not assumed. check_plans() runs every query with its largest and smallest samples and fails on a read that is not index-only, on partitions not pruned, on heap fetches, or on time. It runs on the full twelve months, and nightly in production, so a plan that goes wrong surfaces before a user notices. Cost: time limits depend on the machine (50 ms on the development machine, 150 ms on the server), and a small database can only report, since a sequential scan is the right plan there.

A cache around the fetches and the charts, shared by every session. A fetch another session made is answered from memory, and so is a chart it drew, keyed on the data's window, so a new month starts afresh. It wraps the fetch functions and the drawing of the charts rather than using Shiny's bindCache(), which would keep one session's passing error and serve it to every session. Cost: memory, about 40 KB a chart as the cache counts it; and a lookup no one has made yet still draws its chart, which on the server is most of its time.

One message for each lookup. A view's results sit in a frame that is drawn again only when its layout changes; otherwise only the chart and table in it change, and they are computed with the frame rather than once the browser reports them shown. The chart is drawn at the frame's width, which the browser reports before any pick. So a lookup is one round trip, and a chart is drawn once, not again when a long table gives the page a scroll bar. Cost: a chart is drawn again only when the width moves by 30 px or more, and is scaled to fit until then.

Exports in background workers. A 15-item PowerPoint export takes about three seconds; in the app's process it would freeze every session meanwhile. Exports run on two mirai workers as Shiny ExtendedTasks, each with its own database connection. Cost: each worker holds its own memory, and two workers cap how many exports run at once.

A read-only login. The app connects as a role that can only read the app's views, and refuses to start with a login that can write or reach the loader's tables. The loader and the migrations have roles of their own. Cost: three passwords to manage, and a migration must grant the app what it reads.

Quality

Tests on real data. The tests run against a database that the real loader builds from a sample of the dataset's rows, and compare what the app shows with totals computed independently from the same files. Browser flows drive the app in Chrome. CI fails below 85% line coverage, and also on any function no test reaches. Cost: the tests need PostgreSQL, so CI runs one as a service.

One image, pinned. CI builds its image from the Dockerfile and the R packages' lockfile, tagged by their hash, so the tests, the development stack and production run the same packages; third-party actions are pinned to commit SHAs. Cost: a package update is a rebuild of the image.

Running it

One small server, rebuilt rather than backed up. Production is the database, a jobs container and the app, under Coolify on a shared server with 4 CPUs and 15 GB of memory. Everything in the database comes from the portal, so it is rebuilt, in about an hour, instead of backed up. A scheduled task loads each new month, a nightly one runs the migrations and the plan check, a failure sends an email, and every merge to the main branch is deployed by CI. Cost: if the database is lost, the app is down for about an hour.

Performance, measured. With five users replaying a recorded session against the production server, the median lookup takes 141 ms, network included: a pick is one message, its chart is drawn once, and a chart another session drew comes from the cache. The slowest step is opening a new session, about half a second on the server, and it sets the 95th percentile: 540 ms, against a target of 300 ms; that is the next thing to make faster. The database's part is small: the time is the app's own work in R.

Data: English Prescribing Dataset (EPD) with SNOMED Code, NHSBSA Copyright 2021-2026. Contains public sector information licensed under the Open Government Licence v3.0.

Need a data application like this one? Book a 30-minute call.