有一种方法可以更干净地封装
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)
amdlibrary(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