简体   繁体   English

materialSwitch 在 renderUI 中不起作用

[英]materialSwitch does not work inside a renderUI

I'd like to use shinyWidgets::materialSwitch instead of a checkbox in my app for an improved UI.我想在我的应用程序中使用shinyWidgets::materialSwitch而不是复选框来改进 UI。

However, I can't seem to get materialSwitch to work when used with renderUI/uiOutput.但是,当与 renderUI/uiOutput 一起使用时,我似乎无法让 materialSwitch 工作。 The input displays properly but doesn't seem to register a click to "switch".输入显示正确但似乎没有注册点击“切换”。

For the purposes of my app - I need this to be inside a renderUI.出于我的应用程序的目的 - 我需要将它放在 renderUI 中。

Pkg Versions:封装版本:
shinyWidgets_0.7.2闪亮的Widgets_0.7.2
shiny_1.7.2闪亮_1.7.2

library(shiny)
library(shinyWidgets)
# library(shinyjs)

ui <- fluidPage(
  div(class="row",
    column(width = 3,
      uiOutput("switch")
    )
  )
)

server <- function(input, output, session) {
  
 output$switch = renderUI({
   materialSwitch(
    inputId = "switch",
    label = "Show Count",
    right = TRUE,
    status = "primary",
    value = FALSE
   )
 })
}

shinyApp(ui = ui, server = server)

Why is this happening, and how can the problem be fixed?为什么会发生这种情况,如何解决这个问题?

The issue is that you give same name "switch" to both uiOutput.outputId and materiaSwitch.inputId .问题是您为uiOutput.outputIdmateriaSwitch.inputId赋予相同的名称“switch”。

It works OK when they get different ids:当他们获得不同的 id 时它工作正常:

library(shiny)
library(shinyWidgets)

ui <- fluidPage(
  div(class="row",
      column(width = 3,
             uiOutput("switch"),
             textOutput("result")
      )
  )
)

server <- function(input, output, session) {
  
  output$switch = renderUI({
    materialSwitch(
      inputId = "switchButton",
      label = "Show Count",
      right = TRUE,
      status = "primary",
      value = FALSE
    )
  })
  output$result = renderText(input$switchButton)
}

shinyApp(ui = ui, server = server)

Here is how it should work:这是它应该如何工作:

library(shiny)
library(shinyWidgets)
# library(shinyjs)

ui <- fluidPage(
  div(style = 'position: absolute;left: 50px; top:100px; width:950px;margin:auto',
      materialSwitch(inputId = "switch",
                     label = "Show Count",
                     right = TRUE,
                     status = "primary",
                     value = FALSE)
  )
)

server <- function(input, output, session) {
  
  output$value1 <- renderText({ input$switch })
  
}

shinyApp(ui = ui, server = server)

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM