【问题标题】:R shiny reactivity to trigger only if both inputs changed仅当两个输入都更改时才会触发 R 闪亮反应性
【发布时间】:2020-05-13 08:08:52
【问题描述】:

我有一个相对简单的应用程序,有两个 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 个更改时触发。仅当两个值都更改时,我才需要它打印。

我相信一定有一个非常基本的解决方案,但我找不到。

感谢您的帮助

【问题讨论】:

    标签: r shiny shiny-reactivity


    【解决方案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)
    

    【讨论】:

    • 看起来不错。我不知道你可以在 req.在帮助中,唯一的例子是 req(x)。
    猜你喜欢
    • 2019-06-30
    • 2017-11-24
    • 2020-02-08
    • 2020-08-18
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 2020-12-18
    相关资源
    最近更新 更多