如何小心,正确地将物体封装在r闪亮的模块中? 我试图使我的r闪亮模块尽可能独立,每个模块都可以独立运行以进行测试。我试图最大程度地降低模块之间发生冲突的风险,并紧密地解决...

问题描述 投票:0回答:0
:不太好。

有一种方法可以更干净地封装

toggles
对象和
ui
函数,也许将两个放置在服务器中?我一直在尝试没有运气。
确实有效的事情是在全球环境中定义
toggles
,并将其作为模块参数(使用
createToggle()

toggles

mod30_B_ui <- function(id, toggles)
mod30_B_server <- function(id, toggles)

等),但我发现这种方法笨拙,不想在模块服务器中添加更多参数。另外,我正在尝试在此示例中避免全球环境。

任何建议?

ui <- fluidPage(mod30_B_ui("mod30_B", toggles)
    
这里是一种可能性。  通过使用
mod30_B_server("mod30_B", toggles)
amd
library(shiny)
library(shinyWidgets)

mod30_B_ui <- function(id) {
  ns <- NS(id)
  
  toggles <- c("CS", "WF", "RA")
  
  createToggle <- function(label, id) {
    tags$span(
      prettyCheckbox(
        inputId = id,
        label = label,
        value = TRUE,
        status = "success",
        shape = "curve",
        icon = icon("check")
      )
    )
  }
  
  tagList(
    fluidRow(
      lapply(toggles, function(label) {
        createToggle(label, ns(paste0("toggle_", gsub(" ", "_", tolower(label)))))
      })
    ),
    fluidRow(textOutput(ns("toggleStatus")))
  )
}

mod30_B_server <- function(id) {
  moduleServer(id, function(input, output, session) {
    ns <- NS(id)
    
    toggles <- c("CS", "WF", "RA")
    
    output$toggleStatus <- renderText({
      results <- sapply(toggles, function(label) {
        toggle_id <- paste0("toggle_", gsub(" ", "_", tolower(label)))
        if (input[[toggle_id]]) {
          paste(label, "is checked")
        } else {
          paste(label, "is unchecked")
        }
      })
      paste(results, collapse = "\n")
    })
  })
}

ui <- fluidPage(mod30_B_ui("mod30_B"))

server <- function(input, output, session) {
  mod30_B_server("mod30_B")
}

shinyApp(ui = ui, server = server)
将模块UI创建模块UI的责任委托给UI服务器。 这删除了从模块UI的单个切换的所有引用。

renderUI


r shiny module
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.