clipboard.js is a super light
javascript framework, which provides copy-to-clipboard functionality
using HTML5. The simple rclipboard R package is simple and
leverages clipboard.js functionality to provide a reactive
copy-to-clipboard UI button component, called rclipButton,
and a reactive copy-to-clipboard UI link component, called
rclipLink, for Shiny
R applications.
While rclipButton and rclipLink will work
in applications that use native shiny UI constructors, the
code below illustrates a marginally more complex example in which a
tooltip is displayed when hovering on top of the button thanks to the
use of UI constructors from the bslib package.
library(shiny)
library(bslib)
library(rclipboard)
# The UI
ui <- bslib::page_fluid(
  
  rclipboardSetup(),
  
  # Add a text input
  textInput("copytext", "Copy this:", "Zlika!"),
  
  # UI ouputs for the copy-to-clipboard buttons
  uiOutput("clip"),
  
  # A text input for testing the clipboard content.
  textInput("paste", "Paste here:")
  
)
# The server
server <- function(input, output) {
  
  # Add clipboard buttons
  output$clip <- renderUI({
    rclipButton(
      inputId = "clipbtn",
      label = "rclipButton Copy",
      clipText = input$copytext, 
      icon = icon("clipboard"),
      tooltip = "Click me... I dare you!",
      placement = "top",
      options = list(delay = list(show = 800, hide = 100), trigger = "hover")
    )
  })
  
}
shinyApp(ui = ui, server = server)