我有一个带有两个radioGroupButtons的相对简单的应用程序
radioGroupButtons('rng1', label = 'Select metric',
choices = c('A', 'B', 'C' ),
selected = 'A', justified = TRUE)
radioGroupButtons('rng2', label = 'Select metric',
choices = c('A', 'B', 'C' ),
selected = 'A', justified = TRUE)
我试图围绕observe()或observeEvent()建立反应性,仅当两者都已更改但不起作用时才触发反应性。
因此在以下示例中(尝试了许多其他示例)
observe({
req(input$rng1, input$rng2)
print('both changed')
})
只有1个变化时触发。我只需要在两个值都更改时才能打印。
我相信必须有一个非常基本的解决方案,但找不到它。
感谢您的帮助
答案 0 :(得分:1)
您可以使用reactiveValues
作为radioGroupButtons
中的初始值,并将其设置为A
,然后进行比较
library(shiny)
library(shinyWidgets)
ui <- fluidPage(
radioGroupButtons('rng1', label = 'Select metric',choices = c('A', 'B', 'C' ),selected = 'A', justified = TRUE),
radioGroupButtons('rng2', label = 'Select metric',choices = c('A', 'B', 'C' ),selected = 'A', justified = TRUE)
)
server <- function(input, output,session) {
v <- reactiveValues(rng1 = "A",rng2 = "A")
observeEvent(c(input$rng1,input$rng2),{
req(input$rng1 != v$rng1,input$rng2 != v$rng2)
print('both changed')
v$rng1 <- input$rng1
v$rng2 <- input$rng2
},ignoreInit = TRUE)
}
shinyApp(ui, server)