Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .claude-plugin/marketplace.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@
"skills": [
"./brand-yml",
"./shiny/shiny-bslib",
"./shiny/shiny-bslib-theming"
"./shiny/shiny-bslib-theming",
"./shiny/shiny-r-optimize"
]
},
{
Expand Down
28 changes: 27 additions & 1 deletion shiny/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,40 @@ Comprehensive theming for Shiny apps using bslib and Bootstrap 5. Use when custo
- [Bootswatch themes](https://bootswatch.com/)
- [thematic package](https://rstudio.github.io/thematic/)

### `shiny-r-optimize`

Diagnose and fix performance problems in existing Shiny for R apps — slow startup, sluggish interactions, blocking operations, and apps that need to support more users. Use when an app is slow, hangs, recomputes too much, or must scale beyond prototyping.

**Organization**: SKILL.md provides the diagnostic workflow (understand the complaint → read the app → measure → classify the bottleneck → fix cheapest-first → verify), a quick-wins triage scan, and a symptom-to-fix table. Reference files provide the depth:
- `diagnosis.md` - Profiling with profvis/debrief, reactive-graph inspection with reactlog, micro-benchmarks (bench), load testing (shinyloadtest/shinycannon), the multi-process model, and Connect/Posit Connect Cloud capacity knobs
- `reactive-graph.md` - Narrowing reactive dependencies: shared reactives, req(), isolate(), bindEvent(), freezeReactiveValue(), debounce()/throttle(), timers
- `caching.md` - bindCache() keys and scopes, caching data vs. plots, memoise(), cachem backends, deployment caveats
- `async-tasks.md` - The flush cycle, ExtendedTask, mirai/future_promise/crew, and the hard rules for worker code
- `data-loading.md` - Load-once patterns, fast file formats (fread/feather/parquet/DuckDB), pool + dbplyr, downloads and uploads
- `rendering-ui.md` - Output suspension via tabs, gating with req(), renderUI alternatives, plot/table rendering costs, perceived performance

**Resources** (sources used in developing this skill — useful starting points for optimization work):
- [Shiny performance articles](https://shiny.posit.co/r/articles/improve/)
- [Shiny: Non-blocking operations (async)](https://shiny.posit.co/r/articles/improve/nonblocking/)
- [Shiny: Caching with bindCache](https://shiny.posit.co/r/articles/improve/caching/)
- [Mastering Shiny: Performance](https://mastering-shiny.org/performance.html) and [Scaling](https://mastering-shiny.org/scaling-general.html)
- [promises documentation](https://rstudio.github.io/promises/)
- [profvis](https://rstudio.github.io/profvis/) and [debrief](https://r-lib.github.io/debrief/) for profiling
- [reactlog](https://rstudio.github.io/reactlog/) for reactive-graph inspection
- [shinyloadtest](https://rstudio.github.io/shinyloadtest/) and [shinycannon](https://github.com/rstudio/shinycannon) for load testing
- [mirai](https://mirai.r-lib.org/) and [crew](https://wlandau.github.io/crew/) for async worker backends
- [DuckDB R client](https://r.duckdb.org/) and [pool](https://rstudio.github.io/pool/) for data access
- [Posit Connect scheduler settings](https://docs.posit.co/connect/admin/appendix/off-host-scheduler/)
- [Managing long-running operations in Shiny (Joe Cheng, 2024)](https://opensource.posit.co/resources/videos/2024-05-15_joe-cheng-managing-long-running-operations-in-shiny-posit/)

## Potential Skills

This category could include skills for:

- Shiny app architecture and best practices
- Reactive programming patterns
- UI/UX design for Shiny apps
- Performance optimization
- ~~Performance optimization~~ (covered by `shiny-r-optimize`)
- Testing Shiny applications
- Deployment strategies
- Module development
Expand Down
14 changes: 14 additions & 0 deletions shiny/shiny-r-optimize-workspace/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Iteration results from the skill-creator eval loop (skill-creator convention:
# results live in <skill-name>-workspace/iteration-N/). Too bulky and too
# regenerable to commit; re-run evals to reproduce.
iteration-*/

# Empty scratch dir
measurements/

# Generated fixture data — reproduce with: Rscript generate-data.R
# (deterministic, set.seed(42))
apps/sales-dashboard/sales.csv

# Stray plotting artifact, not a fixture
apps/sales-dashboard/Rplots.pdf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Model Explorer

Fit a response-time model to customer survey data.

The dataset, `survey.csv`, is included in this directory.
70 changes: 70 additions & 0 deletions shiny/shiny-r-optimize-workspace/apps/model-explorer/app.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Model Explorer — fit a response model to survey data
library(shiny)
library(bslib)
library(ggplot2)

ui <- page_fixed(
title = "Model Explorer",
h2("Customer survey response model"),
p("Fit a response-time model to the survey dataset, then explore the results."),
input_task_button("fit", "Fit model"),
p(textOutput("clock", inline = TRUE), style = "color: silver;"),
hr(),
layout_column_wrap(
width = 1 / 2,
card(card_header("Observed vs. predicted"), plotOutput("pred_plot")),
card(card_header("Residual diagnostics"), plotOutput("resid_plot")),
card(card_header("Coefficients"), verbatimTextOutput("coefs")),
card(card_header("Fit summary"), verbatimTextOutput("summary"))
)
)

server <- function(input, output, session) {
survey <- read.csv("survey.csv", stringsAsFactors = FALSE)

# Fit the response-time model. The estimation backend is slow.
fit <- eventReactive(input$fit, {
# ... estimation happens through a slow external service ...
Sys.sleep(4)
lm(response_time ~ age + channel + prior_purchases + satisfaction,
data = survey)
})

output$pred_plot <- renderPlot({
m <- fit()
survey$pred <- predict(m, newdata = survey)
ggplot(survey, aes(pred, response_time)) +
geom_point(alpha = 0.3) +
geom_abline(slope = 1, intercept = 0, color = "red") +
labs(title = "Observed vs. predicted response time", x = "Predicted", y = "Observed")
})

output$resid_plot <- renderPlot({
m <- fit()
ggplot(data.frame(resid = residuals(m), fitted = fitted(m)),
aes(fitted, resid)) +
geom_point(alpha = 0.3) +
geom_hline(yintercept = 0, color = "red") +
labs(title = "Residuals vs. fitted", x = "Fitted", y = "Residual")
})

output$coefs <- renderPrint({
m <- fit()
round(coef(m), 4)
})

output$summary <- renderPrint({
m <- fit()
s <- summary(m)
cat(sprintf("R-squared: %.3f\nResidual SE: %.2f\nObservations: %d\n",
s$r.squared, s$sigma, s$df[2] + s$df[1]))
})

# Live clock
output$clock <- renderText({
invalidateLater(1000)
format(Sys.time(), "%H:%M:%S")
})
}

shinyApp(ui, server)
Loading
Loading