[英]How to select a subset of elements of the input R6 class within a shiny module to perform operations on them
我可以訪問模塊中所有輸入小部件的列表(讓我們將其命名為myModule
)並檢查它們的 state 是否為isTruthy()
。
當我知道(或可以推斷)小部件的確切名稱時,我找到了一種有效的方法(參見1 )。
All_Inputs <- vapply(paste0('axis',1:3),
function(x) { isTruthy(input[[x]]) },
logical(1))
當然,我也可以用一長串if (isTruthy(input$a) && isTruthy(input$b) &&...
來做到這一點。但這兩種解決方案對我來說都不滿意,主要是因為可讀性和可維護性方面的缺陷.
我知道input
class 將它們全部列在以myModule-[AnyName]
開頭的名稱下。 但我不知道如何使用該信息通過循環訪問它們,甚至更好地apply
function。
由於input
是一個命名列表,您可以在names(input)
上使用vapply
:
library(shiny)
counterButton <- function(id, label = "Counter") {
ns <- NS(id)
tagList(
actionButton(ns("button"), label = label),
verbatimTextOutput(ns("out"))
)
}
counterServer <- function(id) {
moduleServer(
id,
function(input, output, session) {
count <- reactiveVal(0)
observeEvent(input$button, {
count(count() + 1)
})
output$out <- renderText({
count()
})
count
}
)
}
ui <- fluidPage(
counterButton("counter1", "Counter #1"),
counterButton("counter2", "Counter #2"),
textOutput('istruthy')
)
server <- function(input, output, session) {
counterServer("counter1")
counterServer("counter2")
output$istruthy <- renderText({
vapply(names(input),
function(x) {
ifelse(startsWith(x, "counter2-"), isTruthy(input[[x]]), TRUE)
},
logical(1))
})
}
shinyApp(ui, server)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.