技能创建器
skill-creator
创建、改进和评估自定义技能,把专业流程转化为可重复使用的 AI 能力。
scaffold-shiny-app【数据与研究】Scaffold a new Shiny application using golem (production R package), rhino (enterprise), or vanilla (quick prototype) structure. Covers framework selection, project initialization, and first module creation. Use when starting a new interactive web application in R, creating a dashboard or data explorer prototype, setting up a production Shiny app as …
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
建新 Shiny 應含產備結構用 golem、rhino、或素架。
按項需選宜框:
| 框 | 宜 | 結構 |
|---|---|---|
| golem | 產應運為 R 包 | R 包含 DESCRIPTION、測、誌 |
| rhino | 企應含 JS/CSS 建管 | box 模、Sass、JS 包、rhino::init() |
| 素 | 速原型與學 | 單 app.R 或 ui.R/server.R 對 |
得:明框決於項範與組需。
敗:未定→默 golem—予最多結構可後簡。素僅宜棄原型。
golem::create_golem("myapp", package_name = "myapp")
建:
myapp/
├── DESCRIPTION
├── NAMESPACE
├── R/
│ ├── app_config.R
│ ├── app_server.R
│ ├── app_ui.R
│ └── run_app.R
├── dev/
├── inst/
├── man/
├── tests/
└── vignettes/
rhino::init("myapp")
建:
myapp/
├── app/
│ ├── js/
│ ├── logic/
│ ├── static/
│ ├── styles/
│ ├── view/
│ └── main.R
├── tests/
├── 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"))。素→確 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 與服函用正命空。
敗:確模 UI 函諸入/出 ID 用 NS(id)。無命空→ID 多用時撞。
# Golem
golem::run_dev()
# Rhino
shiny::runApp()
# Vanilla
shiny::runApp("app.R")
得:應於瀏無誤啟。
敗:察 R 控制台誤訊。常題:缺包(裝之)、口已用(port = 3839)、UI/服碼語誤。
shiny::runApp() 啟無誤NS(id) 命空隔inputId、outputId 必裹 ns()。忘致默 ID 撞devtools::load_all()、devtools::test()、devtools::document(),非 source()library()—用 box::use() 顯入build-shiny-moduletest-shiny-appdeploy-shiny-appdesign-shiny-uicreate-r-packagemanage-renv-dependencies