关于重新实现Shiny的dueling select box及打包指南的技术咨询
Got it, let's walk through exactly how to package that dueling select box (the chooser component from the Shiny example) into a distributable package—since the original explanation cut off mid-sentence, I'll fill in all the missing details to get you sorted:
First, create a folder for your package with these required subdirectories and files:
R/: For all your R code functionsinst/www/: For frontend assets (JS, CSS, images)DESCRIPTION: Metadata about your package (name, author, dependencies)NAMESPACE: Defines which functions are visible to users
- Move the
chooser.Rfile from the example directly into theR/directory. This file should contain your core input function (likechooserInput()) and any update functions (likeupdateChooserInput()). - Take all the files from the example's
www/folder (e.g.,chooser.js,chooser.css) and put them intoinst/www/in your package. This is the standard location for Shiny web resources in R packages—Shiny automatically recognizes this path.
Add metadata so R knows about your package. Here's a template to adapt:
Package: shinyDuelSelect Title: A Dueling Select Box Input for Shiny Apps Version: 0.1.0 Authors@R: person("Your Name", "your.email@example.com", role = c("aut", "cre")) Description: A custom Shiny input component that lets users move items between two select boxes, perfect for multi-item selection workflows. Depends: shiny (>= 1.0.0) License: MIT Encoding: UTF-8 LazyData: true
Export the functions users need to call, so they can access them when loading your package. Add these lines:
export(chooserInput) export(updateChooserInput) import(shiny)
The import(shiny) line ensures your package has access to Shiny's internal functions.
Create a new file in R/ (e.g., register_resources.R) to tell Shiny where to find your JS/CSS files:
#' @import shiny .onLoad <- function(libname, pkgname) { shiny::addResourcePath( prefix = "duelselect", directoryPath = system.file("www", package = pkgname) ) }
This maps the inst/www/ folder to a URL prefix (duelselect) that your chooserInput() function can reference when loading assets.
Use devtools::load_all() to load your package locally, then test it with a minimal Shiny app:
library(shiny) library(shinyDuelSelect) ui <- fluidPage( chooserInput( "mySelector", "Pick Your Items", leftChoices = c("Apple", "Banana", "Cherry"), rightChoices = c(), size = 4, multiple = TRUE ) ) server <- function(input, output, session) { observe(print(input$mySelector)) # Check if selected items are captured } shinyApp(ui, server)
- Run
devtools::build()to generate a installable.tar.gzfile. Users can install it withinstall.packages("shinyDuelSelect_0.1.0.tar.gz", repos = NULL). - If you want to share it publicly, you can push it to GitHub (users can install with
devtools::install_github("yourusername/shinyDuelSelect")) or submit it to CRAN following their guidelines.
To clarify the original cut-off explanation: the "contents of www would be mo..." was referring to moving those files into inst/www/—the standard home for web assets in R packages.
内容的提问来源于stack exchange,提问作者polgia0

