clipboard.js 在闪亮的应用程序中

clipboard.js in Shiny application

当使用 clipboard.js 单击按钮时,我正在尝试复制到剪贴板 'Clipboard copied !'。

未复制任何内容,出现 JS 错误:Uncaught TypeError: Cannot read property 'addEventListener' of null

library(shiny.semantic)

ui <- semanticPage(
    tags$head(
        tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"),
        tags$script(HTML("new ClipboardJS('.clipboardButton');"))
    ),
    
    action_button(input_id="myButton",
                  class = "clipboardButton",
                  label = "Click Me !",
                  `data-clipboard-text` = "Clipboard copied !"
    ),
    text_input("text",
               placeholder="paste here to check"
    )
)

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

shinyApp(ui, server)

你必须添加一个$(document).ready:

  tags$head(
    tags$script(src = "https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js"),
    tags$script(HTML(
      "$(document).ready(function(){new ClipboardJS('.clipboardButton');});"
    ))
  ),