Created
March 24, 2021 19:35
-
-
Save edavidaja/fc92d9b5a264f108f529e28041dd0aad to your computer and use it in GitHub Desktop.
shiny + the clipboard api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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