为什么toggleState不modalDialog R中闪亮的工作吗?

问题描述 投票:0回答:1

当我使用shinyjs :: togglestate所有输入变量从与modalDialog的输入变量的UI相比,它仅适用于第一次。如果我关闭模态对话框,然后再次打开它,它不工作!

反应性规则是:

if they are equal: button is blocked
if they are different: the button is enabled

OBS:从模态对话框变量的值必须在UI选用的输入值。 (这是非常黎民例如,如果你有一个数据库检查,你只是想更新,如果有什么改变)

我创造了这个简单的应用程序来代表问题。只要运行它,点击一次按钮,观察预期的行为,关闭模态对话框,单击第二次看的是它不工作了,但是如果你在模态对话框更改一些值,并回到原来的输入值突然“记住”重新工作。

library(shiny)
library(shinyjs)

shinyApp(
  ui =
    fluidPage(
      useShinyjs(),
      sidebarLayout(
        sidebarPanel(
                     textInput("txt1","text"),
                     selectInput("select1","select",c("A","B","C")),
                     numericInput("n1","numeric",1),
                     dateInput("d1","date",value = Sys.Date()),

                     actionButton("go", "Go")),
      mainPanel())
    ),

  server =
    function(input, output, session) {


      observe({
        shinyjs::toggleState("click", !(input$txt1    == input$txt2 &&
                                        input$select1 == input$select2 &&
                                        input$n1      == input$n2 &&
                                        input$d1      == input$d2  
                                        ) )
        })

observeEvent(input$go,{
  showModal(
    modalDialog(
      textInput("txt2", "text", input$txt1),
      selectInput("select2", "select", c("A","B","C"), input$select1),
      numericInput("n2", "numeric", input$n1 ),
      dateInput("d2", "date", value = input$d1),

      actionButton("click", "Click")
    )
  )

})


    }
)

为什么有这样的意外行为?是否有任何解决方法吗?

先感谢您 !

r shiny modal-dialog shinyjs
1个回答
0
投票

在你的代码的问题是,observe评估仅在创建输入时,则当输入改变。你们需要点击进入每个时间来评估你的切换状态。所以,你需要在除了使用observeEvent的使用observe。修改后的服务器代码如下所示:

server =
function(input, output, session) {

  observe({
    shinyjs::toggleState("click", !(input$txt1    == input$txt2 &&
                                      input$select1 == input$select2 &&
                                      input$n1      == input$n2 &&
                                      input$d1      == input$d2
    ) )
  })

  observeEvent(input$go,{
    showModal(
      modalDialog(
        textInput("txt2", "text", input$txt1),
        selectInput("select2", "select", c("A","B","C"), input$select1),
        numericInput("n2", "numeric", input$n1 ),
        dateInput("d2", "date", value = input$d1),

        actionButton("click", "Click")
      )
    )

  })

  observeEvent(input$go,{
    shinyjs::toggleState("click", !(input$txt1    == input$txt2 &&
                                      input$select1 == input$select2 &&
                                      input$n1      == input$n2 &&
                                      input$d1      == input$d2
    ) )
  })


}

希望能帮助到你!

© www.soinside.com 2019 - 2024. All rights reserved.