You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于重新实现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:

Step 1: Set Up the Standard R Package Structure

First, create a folder for your package with these required subdirectories and files:

  • R/: For all your R code functions
  • inst/www/: For frontend assets (JS, CSS, images)
  • DESCRIPTION: Metadata about your package (name, author, dependencies)
  • NAMESPACE: Defines which functions are visible to users
Step 2: Place Your Component Files Correctly
  • Move the chooser.R file from the example directly into the R/ directory. This file should contain your core input function (like chooserInput()) and any update functions (like updateChooserInput()).
  • Take all the files from the example's www/ folder (e.g., chooser.js, chooser.css) and put them into inst/www/ in your package. This is the standard location for Shiny web resources in R packages—Shiny automatically recognizes this path.
Step 3: Fill Out the DESCRIPTION File

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
Step 4: Configure the NAMESPACE File

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.

Step 5: Register Frontend Resources

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.

Step 6: Test Your Package

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)
Step 7: Build and Distribute
  • Run devtools::build() to generate a installable .tar.gz file. Users can install it with install.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:28:37