关于Shinymanager与Golem框架对接的技术咨询
Absolutely! Integrating shinymanager with the golem framework is totally feasible—here's a step-by-step guide with code snippets tailored to your setup, where credentials are stored in a credentials.sqlite file in your package directory.
Step 1: Organize Your Credential File
First, place your credentials.sqlite file in the inst/ directory of your golem project. Golem automatically includes files from inst/ in the installed package, so this ensures your credential database will be accessible when the app runs (either in development or after installation).
Step 2: Secure the UI (in app_ui.R)
Wrap your golem app's UI inside shinymanager::secure_app() just like you would in a traditional Ui.R/Server.R setup. Here's how to modify the default app_ui function:
#' The application User-Interface #' #' @param request Internal parameter for `{shiny}`. DO NOT REMOVE. #' @import shiny #' @import shinymanager #' @noRd app_ui <- function(request) { tagList( golem_add_external_resources(), # Keep this for external assets # Wrap your app UI in secure_app() secure_app( fluidPage( h1("My Secured Golem Application"), # Add your golem modules here mod_dashboard_ui("dashboard_module_1") ) ) ) }
Step 3: Handle Authentication in the Server (in app_server.R)
Use shinymanager::secure_server() to validate credentials against your SQLite database. We'll use system.file() to safely reference the database file in your package's inst/ directory:
#' The application server-side #' #' @param input,output,session Internal parameters for `{shiny}`. DO NOT REMOVE. #' @import shiny #' @import shinymanager #' @noRd app_server <- function(input, output, session) { # Authenticate users against the SQLite database auth_result <- secure_server( check_credentials = check_credentials( db = system.file("credentials.sqlite", package = "your_package_name") ) ) # Optional: Access authenticated user info observe({ user_info <- reactiveValuesToList(auth_result) cat("Logged in user:", user_info$user, "\n") }) # Run your golem modules as usual callModule(mod_dashboard_server, "dashboard_module_1") }
Key Notes
- Replace
your_package_namewith the actual name of your golem package. - During development, you can run the app with
golem::run_app()—thesystem.file()call will still find the database ininst/. - If you update
credentials.sqlite, restart your R session or reload the package to pick up changes.
内容的提问来源于stack exchange,提问作者Sofiane M'barki

