在Shiny应用中相互更新材料开关

huangapple go评论64阅读模式
英文:

Mutually update material switch in shiny app

问题

我有两个开关。我希望每个开关在它们变为TRUE时强制另一个开关变为FALSE(例如,如果你切换 switch1(= TRUE),并且 switch2 = TRUE,那么 switch2 应该被更改为 FALSE)。

这是我尝试实现的简化版本:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)

ui <- fluidPage(
  materialSwitch(inputId = "switch1", label = "开关1", status = "danger"),
  materialSwitch(inputId = "switch2", label = "开关2", status = "danger")
)

server <- function(input, output, session) {

  observeEvent(input$switch1, {
    if (input$switch1 == TRUE && input$switch2 == TRUE) {
      updateMaterialSwitch(session = session, "switch2", status = "danger", value = FALSE)
    }
  },
  ignoreInit = TRUE)
}

shinyApp(ui, server)

我尝试将输入传递给一个响应式事件,但每次我在 switch2 被触发后触发 switch1 时,应用程序都会进入一个无限循环。有什么建议吗?

最终目标是使条件双向工作,但目前由于它甚至不在一个方向上工作,我会感激一些帮助。

英文:

I have to swtiches. I want each switch to force the other one to FALSE should they become TRUE. (e.g. if you 'toggle' switch1 (= TRUE), and switch2 = TRUE, then switch2 should be changed to FALSE).

This is a simplified version of what I'm trying to achieve:

library(shiny)
library(shinydashboard)
library(shinydashboardPlus)


ui &lt;- fluidPage(
  materialSwitch(inputId = &quot;switch1&quot;, label = &quot;Switch 1&quot;, status = &quot;danger&quot;),
  materialSwitch(inputId = &quot;switch2&quot;, label = &quot;Switch 2&quot;, status = &quot;danger&quot;)
)

server &lt;- function(input, output, session) {

  

  
  observeEvent(input$switch1, {
    #if (is.null(input$switch1)) return(NULL)
    if (input$switch1 == TRUE &amp;&amp; input$switch2 == TRUE) {
      updateMaterialSwitch(session = session, &quot;switch2&quot;, status = &quot;danger&quot;, value = FALSE)
    }
  },
  ignoreInit = TRUE)
  

}

shinyApp(ui, server)

I tried passing the input to a reactive event, but everytime I trigger switch1 after switch2 has been triggered, the app enters an endless loop. Any suggestions?

The end game would be for condition to work both ways, but for now since it doens't even work in one direction I would appreciate some help there.

答案1

得分: 1

请检查以下内容:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  materialSwitch(inputId = "switch1", label = "开关 1", status = "危险"),
  materialSwitch(inputId = "switch2", label = "开关 2", status = "危险")
)

server <- function(input, output, session) {
  observeEvent(input$switch1, {
    if (input$switch1 == TRUE && input$switch2 == TRUE) {
      updateMaterialSwitch(session = session, "switch2", value = FALSE)
    }
  })
  observeEvent(input$switch2, {
    if (input$switch1 == TRUE && input$switch2 == TRUE) {
      updateMaterialSwitch(session = session, "switch1", value = FALSE)
    }
  })
}

shinyApp(ui, server)
英文:

Please check the following:

library(shiny)
library(shinyWidgets)

ui &lt;- fluidPage(
  materialSwitch(inputId = &quot;switch1&quot;, label = &quot;Switch 1&quot;, status = &quot;danger&quot;),
  materialSwitch(inputId = &quot;switch2&quot;, label = &quot;Switch 2&quot;, status = &quot;danger&quot;)
)

server &lt;- function(input, output, session) {
  observeEvent(input$switch1, {
    if (input$switch1 == TRUE &amp;&amp; input$switch2 == TRUE) {
      updateMaterialSwitch(session = session, &quot;switch2&quot;, value = FALSE)
    }
  })
  observeEvent(input$switch2, {
    if (input$switch1 == TRUE &amp;&amp; input$switch2 == TRUE) {
      updateMaterialSwitch(session = session, &quot;switch1&quot;, value = FALSE)
    }
  })
}

shinyApp(ui, server)

huangapple
  • 本文由 发表于 2023年2月8日 20:52:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75386076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定