我怎样才能用observeEvent捕获HTML中闪亮的输入id呢?

cyej8jka  于 2022-12-02  发布在  其他
关注(0)|答案(2)|浏览(116)

我怎样才能用observeEvent捕获HTML中闪亮的输入id呢?

shinyApp(
  ui = basicPage( HTML('<input type="button" name = "b1" value="Travel time"/>')),

  server = function(input, output, session) {
    observeEvent(input$b1, {
      print(paste("This will only be printed once; all",
                  "subsequent button clicks won't do anything"))
    }, once = TRUE)

  }
)

我想使用HTML实现该功能,当我点击“旅行时间”时,可以观察到该事件。

dbf7pr2w

dbf7pr2w1#

无需添加软件包:

shinyApp(
  ui = basicPage( 
    HTML('<button type="button" id="b1" class="action-button">Travel time</button>')
  ),
  
  server = function(input, output, session) {
    observeEvent(input$b1, {
      print(paste("This will only be printed once; all",
                  "subsequent button clicks won't do anything"))
    }, once = TRUE) 
  }

)
6gpjuf90

6gpjuf902#

您可以使用shinyjs包来解决此问题

shinyApp(
  ui = basicPage(
    shinyjs::useShinyjs(),
    HTML('<input type="button" id = "b1" value="Travel time"/>')
  ),
  
  server = function(input, output, session) {
    shinyjs::onclick(
      "b1",
      {
        shinyjs::disable(id = "b1")
        print(paste("This will only be printed once; all",
                    "subsequent button clicks won't do anything"))
      }
    )
    
  }
)

希望这对你有帮助!

相关问题