协作头脑风暴
brainstorming
在实现前澄清需求、探索设计选择,并形成清晰的开发方向。
scaffold-shiny-app【开发与工程】Scaffold new Shiny app → golem (prod R pkg)|rhino (enterprise)|vanilla (quick proto). Framework select, init, first module. Use → new interactive R web app, dashboard|explorer proto, prod Shiny as R pkg w/ golem, enterprise Shiny w/ rhino.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
Create new Shiny w/ prod-ready structure → golem|rhino|vanilla.
| Framework | Best For | Structure |
|---|---|---|
| golem | Production apps shipped as R packages | R package with DESCRIPTION, tests, vignettes |
| rhino | Enterprise apps with JS/CSS build pipeline | box modules, Sass, JS bundling, rhino::init() |
| vanilla | Quick prototypes and learning | Single app.R or ui.R/server.R pair |
→ Clear decision by scope + team needs.
If err: unsure → default golem (most structure, can simplify). Vanilla only for throwaway protos.
golem::create_golem("myapp", package_name = "myapp")
Creates:
myapp/
├── DESCRIPTION
├── NAMESPACE
├── R/
│ ├── app_config.R
│ ├── app_server.R
│ ├── app_ui.R
│ └── run_app.R
├── dev/
│ ├── 01_start.R
│ ├── 02_dev.R
│ ├── 03_deploy.R
│ └── run_dev.R
├── inst/
│ ├── app/www/
│ └── golem-config.yml
├── man/
├── tests/
│ ├── testthat.R
│ └── testthat/
└── vignettes/
rhino::init("myapp")
Creates:
myapp/
├── app/
│ ├── js/
│ ├── logic/
│ ├── static/
│ ├── styles/
│ ├── view/
│ └── main.R
├── tests/
│ ├── cypress/
│ └── testthat/
├── .github/
├── app.R
├── dependencies.R
├── rhino.yml
└── renv.lock
Create app.R:
library(shiny)
library(bslib)
ui <- page_sidebar(
title = "My App",
sidebar = sidebar(
sliderInput("n", "Sample size", 10, 1000, 100)
),
card(
card_header("Output"),
plotOutput("plot")
)
)
server <- function(input, output, session) {
output$plot <- renderPlot({
hist(rnorm(input$n), main = "Random Normal")
})
}
shinyApp(ui, server)
→ Project dir created w/ all scaffold files.
If err: golem → install.packages("golem"). Rhino → remotes::install_github("Appsilon/rhino"). Vanilla → ensure shiny+bslib installed.
# Initialize renv
renv::init()
# Add core dependencies
usethis::use_package("shiny")
usethis::use_package("bslib")
usethis::use_package("DT") # if using data tables
usethis::use_package("plotly") # if using interactive plots
# Snapshot
renv::snapshot()
Deps managed in dependencies.R:
# dependencies.R
library(shiny)
library(bslib)
library(DT)
→ All deps recorded in DESCRIPTION (golem) | dependencies.R (rhino) + renv-locked.
If err: renv::init() fails → check write perms. Pkg install fails → check R ver compat.
golem::add_module(name = "dashboard", with_test = TRUE)
Creates R/mod_dashboard.R + tests/testthat/test-mod_dashboard.R.
Create app/view/dashboard.R:
box::use(
shiny[moduleServer, NS, tagList, h3, plotOutput, renderPlot],
)
#' @export
ui <- function(id) {
ns <- NS(id)
tagList(
h3("Dashboard"),
plotOutput(ns("plot"))
)
}
#' @export
server <- function(id) {
moduleServer(id, function(input, output, session) {
output$plot <- renderPlot({
plot(1:10)
})
})
}
Add to R/mod_dashboard.R:
dashboardUI <- function(id) {
ns <- NS(id)
tagList(
h3("Dashboard"),
plotOutput(ns("plot"))
)
}
dashboardServer <- function(id) {
moduleServer(id, function(input, output, session) {
output$plot <- renderPlot({
plot(1:10)
})
})
}
→ Module file w/ UI+server using proper namespacing.
If err: ensure NS(id) for all input/output IDs in UI fn. Without → IDs collide on multi-use.
# Golem
golem::run_dev()
# Rhino
shiny::runApp()
# Vanilla
shiny::runApp("app.R")
→ App launches in browser w/o errs.
If err: check R console. Common: missing pkgs (install), port in use (port = 3839), syntax errs in UI/server.
shiny::runApp() launches w/o errsNS(id) for namespace isolationinputId+outputId must wrap ns(). Forget → silent ID collisions.devtools::load_all(), test(), document() — not source().library() — use box::use().build-shiny-module — reusable modules w/ namespace isolationtest-shiny-app — shinytest2 + testServer() testsdeploy-shiny-app — deploy to shinyapps.io, Posit Connect, Dockerdesign-shiny-ui — bslib theming + responsivecreate-r-package — R pkg scaffold (golem apps are R pkgs)manage-renv-dependencies — detailed renv mgmt