brainstorming
brainstorming
You MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).
scaffold-shiny-app使用 golem(生产 R 包)、rhino(企业级)或 vanilla(快速原型)结构搭建新的 Shiny 应用程序。涵盖框架选择、项目初始化及第一个模块创建。适用于在 R 中 启动新的交互式 Web 应用、创建仪表盘或数据探索器原型、将生产 Shiny 应用 以 golem 方式设置为 R 包,或用 rhino 引导企业级 Shiny 项目。
Independent GitHub snapshot · Automated classification · Original documentation governs
使用 golem、rhino 或 vanilla 脚手架创建具有生产就绪结构的新 Shiny 应用程序。
根据项目需求评估并选择合适的框架:
| 框架 | 最适用于 | 结构 |
|---|---|---|
| golem | 以 R 包形式交付的生产应用 | 包含 DESCRIPTION、测试、vignette 的 R 包 |
| rhino | 带 JS/CSS 构建管道的企业应用 | box 模块、Sass、JS 打包、rhino::init() |
| vanilla | 快速原型和学习 | 单个 app.R 或 ui.R/server.R 对 |
预期结果: 基于项目范围和团队需求做出明确的框架决策。
失败处理: 如果不确定,默认选择 golem——它提供最多结构,后期可简化。vanilla 仅适用于一次性原型。
golem::create_golem("myapp", package_name = "myapp")
这会创建:
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")
这会创建:
myapp/
├── app/
│ ├── js/
│ ├── logic/
│ ├── static/
│ ├── styles/
│ ├── view/
│ └── main.R
├── tests/
│ ├── cypress/
│ └── testthat/
├── .github/
├── app.R
├── dependencies.R
├── rhino.yml
└── renv.lock
创建 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)
预期结果: 项目目录已创建,包含所有脚手架文件。
失败处理: 对于 golem,确保已安装 golem 包:install.packages("golem")。对于 rhino,从 GitHub 安装:remotes::install_github("Appsilon/rhino")。对于 vanilla,确保 shiny 和 bslib 已安装。
# 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()
依赖项在 dependencies.R 中管理:
# dependencies.R
library(shiny)
library(bslib)
library(DT)
预期结果: 所有依赖项记录在 DESCRIPTION(golem)或 dependencies.R(rhino)中,并用 renv 锁定。
失败处理: 如果 renv::init() 失败,检查写入权限。如果包安装失败,检查 R 版本兼容性。
golem::add_module(name = "dashboard", with_test = TRUE)
这会创建 R/mod_dashboard.R 和 tests/testthat/test-mod_dashboard.R。
创建 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)
})
})
}
将模块函数添加到单独的文件 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)
})
})
}
预期结果: 模块文件已创建,包含使用正确命名空间的 UI 和 server 函数。
失败处理: 确保模块在 UI 函数中使用 NS(id) 处理所有输入/输出 ID。如果没有命名空间,多次使用该模块时 ID 会冲突。
# Golem
golem::run_dev()
# Rhino
shiny::runApp()
# Vanilla
shiny::runApp("app.R")
预期结果: 应用程序在浏览器中启动,无错误。
失败处理: 检查 R 控制台中的错误消息。常见问题:缺少包(安装它们)、端口已被占用(用 port = 3839 指定不同端口),或 UI/server 代码中存在语法错误。
shiny::runApp() 启动无错误NS(id) 进行正确的命名空间隔离inputId 和 outputId 都必须用 ns() 包裹。遗漏会导致静默的 ID 冲突。devtools::load_all()、devtools::test() 和 devtools::document()——而非 source()。library() 调用——使用 box::use() 进行显式导入。build-shiny-module — 创建具有正确命名空间隔离的可复用 Shiny 模块test-shiny-app — 设置 shinytest2 和 testServer() 测试deploy-shiny-app — 部署到 shinyapps.io、Posit Connect 或 Dockerdesign-shiny-ui — bslib 主题和响应式布局设计create-r-package — R 包脚手架(golem 应用是 R 包)manage-renv-dependencies — 详细的 renv 依赖管理