Skip to content

Instantly share code, notes, and snippets.

@edavidaja
Created March 24, 2021 19:35
Show Gist options
  • Save edavidaja/fc92d9b5a264f108f529e28041dd0aad to your computer and use it in GitHub Desktop.
Save edavidaja/fc92d9b5a264f108f529e28041dd0aad to your computer and use it in GitHub Desktop.
shiny + the clipboard api
library(shiny)
ui <- fluidPage(
# Application title
titlePanel("clippers"),
sidebarLayout(
sidebarPanel(
textAreaInput("text_input", "enter text:"),
# using submit button makes the copy less of a lie (but still a lie) because
# the clipboard won't be updated until the text output is rendered
submitButton("copy text:", icon = icon("copy"))
),
mainPanel(
verbatimTextOutput("clippers")
)
),
tags$script(src = "clip.js")
)
# Define server logic required to draw a histogram
server <- function(input, output) {
output$clippers <- renderText({
input$text_input
})
}
# Run the application
shinyApp(ui = ui, server = server)
// www/clip.js
async function cp(copytarget) {
try {
await navigator.clipboard.writeText(copytarget)
console.log("copied text");
} catch (err) {
console.err("failed to copy:", err)
}
}
$("#clippers").on("shiny:value", function(event) {
cp(event.value)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment