英文:
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 <- fluidPage(
materialSwitch(inputId = "switch1", label = "Switch 1", status = "danger"),
materialSwitch(inputId = "switch2", label = "Switch 2", status = "danger")
)
server <- function(input, output, session) {
observeEvent(input$switch1, {
#if (is.null(input$switch1)) return(NULL)
if (input$switch1 == TRUE && input$switch2 == TRUE) {
updateMaterialSwitch(session = session, "switch2", status = "danger", 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 <- fluidPage(
materialSwitch(inputId = "switch1", label = "Switch 1", status = "danger"),
materialSwitch(inputId = "switch2", label = "Switch 2", status = "danger")
)
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论