Skip to content

Shiny app organizational code refactor - #126

Merged
Nick-Eagles merged 9 commits into
develfrom
app_refactor
Nov 11, 2026
Merged

Nick-Eagles merged 9 commits into
develfrom
app_refactor

Conversation

@Nick-Eagles

Copy link
Copy Markdown
Member

Purpose of this pull request

This pull request does a full refactor of Shiny-app code used by run_app()
without changing functionality (except for a couple bug fixes that resulted
naturally from the refactor). In contrast to the original large app_server.R
and app_ui.R scripts, this PR implements several new app_*.R files. The
overall goal was to improve readability, modularity, and maintainability in
the future, particularly with major upcoming feature changes in mind.

Why such an extreme rewrite?

While not a particularly interesting PR on its own, I see it as an important step for future feature updates to build upon.

What exactly changes in this PR?

Organizational improvements

The most important organizational change is delegating logical functional units of the app to individual app_*.R scripts, individually described here. For example, layer-level functionality goes into clearly named app_layer_*.R scripts, and spot-level functionality into app_spot_*.R. Within those units, dropdown features of the app go into app_(layer|spot)_controls.R. Issues we've had in this package related to colors (e.g. part of this PR) naturally go away by delegating color-related code to a dedicated app_palettes.R script.

Next, app_server.R becomes short and elegant, outsourcing individual components to respective R files (such as in this code block), and something nearly identical conceptually happens in app_ui.R.

Bug fixes

These are interesting because they naturally became exposed and corrected as a direct consequence of code organization improvements, rather than due to intentional targeted fixes.

  • Default cluster colors correctly pass to gene set enrichment and spatial registration plots in the layer-level tab (this is less important, as Manisha fixed something similar here)
  • A previous mismatch of in-app vs exported PDF boxplots in the model boxplots tab is now corrected
  • Selecting a non-default sample now works correctly when editing the histology image

New tests

Using a new tiny example dataset, I built and individually verified new tests, most notably those in test-app-features.R. The idea here was to break the app into (29) individual functions, and do a tiny test checking a very basic version of each function. These are not intended to be comprehensive, but are meant to quickly automatically check that nothing major and obvious breaks in future changes to the app. As I'll describe later, I also manually inspected the app thoroughly after this PR's changes.

How do we know such a substantial rewrite didn't break anything?

As previously mentioned, the new automated tests verify a small fraction of the functionality of the app. I also thoroughly manually interacted with a copy of the NAc app, comparing the existing app with a version running the new spatialLIBD. In detail, here's what I checked:

Spot-level data

  • Every toggle/dropdown has identical-looking behavior. For example tested complex interactions of features, like percentile-filtered Z-scored + PCA features
  • Lasso and click-based annotation and annotation download have identical behavior (in clusters (interactive))
  • Weirdly enough trying to overwrite manual annotation with an external CSV broke the existing app as well as the refactored app (might've done something wrong here)
  • Clusters + gene grid (static) arranges multiple samples, updates, and produces a PDF identically
  • Subsetting clusters works as expected in gene (interactive)
  • Under edit image, didn’t comprehensively test every setting, but set brightness to 50, normalized, and negated image. The displayed and downloaded PDF were identical
  • Bug fix: under “edit image”, selecting a different sample than the default now works

Layer-level data

  • (Reduced Dim) Every toggle/dropdown has identical (correct) behavior
  • (Model boxplots) Every toggle/dropdown/search has identical behavior (tested this with enrichment results, but ANOVA + pairwise also look the same— didn’t test every setting as thoroughly with these though). All PDF and CSV downloads are identical except for one bug fix: PDF and in-app boxplots now match rather than having a different x-axis
  • (Gene set enrichment) Refactor actually independently fixed default colors in heatmap (much like Manisha’s recent pull request). CSV + plot downloads work identically (aside from the color fix)
  • (Spatial registration) Refactor again fixed default colors in the heatmap. Changing all 3 toggles worked. PDF and CSV downloads worked as expected. Did not test CSV upload

Other miscellaneous details

I used AI to help out with many of these changes, and initially overlooked that this commit touched files that weren't strictly necessary for the app-related changes. Rather than undoing the commit, I undid the changes with a new commit. The PR therefore might should edits to additional files that weren't really changed overall (a diff between the final PR and devel should show no changes in those files).

@Nick-Eagles

Copy link
Copy Markdown
Member Author

Forgot to mention a small change not described above: I reimplemented a base-R plot (layer-level boxplots) in ggplot2, which helped ensure plots didn't overlap app text (previously margins were hardcoded). Changes can be seen in R/layer_boxplot.R

@Nick-Eagles
Nick-Eagles merged commit b990649 into devel Nov 11, 2026
11 checks passed
@Nick-Eagles
Nick-Eagles deleted the app_refactor branch November 11, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant