R 闪亮的反应性仅在两个输入都发生变化时才会触发
R shiny reactivity to trigger only if both inputs changed
我有一个相对简单的应用,有两个 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 个变化时触发。我需要它仅在两个值都更改时打印。
我相信一定有一个非常基本的解决方案,但我找不到。
感谢您的帮助
您可以使用 reactiveValues
作为设置为 A
的 radioGroupButtons
的初始值,然后比较它
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)
我有一个相对简单的应用,有两个 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 个变化时触发。我需要它仅在两个值都更改时打印。
我相信一定有一个非常基本的解决方案,但我找不到。
感谢您的帮助
您可以使用 reactiveValues
作为设置为 A
的 radioGroupButtons
的初始值,然后比较它
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)