1

I am trying to implement a R Shiny app that will automatically read the content of the clipboard when the users hit "ctrl+c". In the app, I need to somehow observe the change of clipboard content. I googled but with no luck.

I know that content of the clipboard can be assessed via readClipboard() function, but I don't know how to detect the change in clipboard in a R Shiny App.

Below I have show some simple R Shiny code to observe a button click and update the text output; but I don't know how to observe a change in Clipboard.

Thank you for all your help in advance.

ui <- fluidPage(
  actionButton(inputId = "dummy_button", label = "This is a button"),
  verbatimTextOutput(outputId = "detector")
)



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

# create a reactiveVal of count
  counter <- reactiveVal(0)

  observeEvent(input$dummy_button, {
#update the counter after button click
    counter(counter() + 1) 
    output$detector <- renderPrint({
      paste("button clicked", counter(), "times")
  })
})

}

# Run the application 
shinyApp(ui = ui, server = server)

The text output should be something like "The content in clipboard has changed x times"

1 Answers1

1

I'm not sure there's an R function to do this for you. Instead, I think a custom message handler from the browser is what you need.

1) Add the script to your UI that listens for a key press and sends a message to Shiny. JavaScript borrowed from SO answer to this question: How to detect Ctrl+V, Ctrl+C using JavaScript?

2) Use the observer to listen for the custom message handler.

library(shiny)
ui <- fluidPage(
  actionButton(inputId = "dummy_button", label = "This is a button"),
  verbatimTextOutput(outputId = "detector"),
  tags$script('
              var counter = 0;
              var ctrlDown = false,
              ctrlKey = 17,
              cKey = 67;
              $(document).keydown(function(e) {
              if (e.keyCode == ctrlKey ) ctrlDown = true;
              }).keyup(function(e) {
              if (e.keyCode == ctrlKey ) {
                ctrlDown = false;
                counter = counter + 1
               Shiny.onInputChange("mydata",counter );
              }

              });
              ') 
  )



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

  counter <- reactiveVal(0)

  observeEvent(input$mydata,{
    counter(counter() + 1)

  })

  output$detector <- renderPrint({
    req(input$mydata)
    paste("button clicked", counter(), "times")
  })

}

# Run the application
shinyApp(ui = ui, server = server)
Ryan Morton
  • 2,605
  • 1
  • 16
  • 19
  • Ryan, Thank you for your answers. The code works well, but only when you are at the browser windows in which is R shiny app is ran. If you do ctrl+c when the Rshiny app is minimized, the counter won't grow as I observed. But this is a really good starting point with the javascript. – Porkbun-Wei Aug 21 '19 at 02:39
  • Yes, it'll only observe what happens in the browser, so minimizing the browser won't catch it. Reading the local machine clipboard is going to present several problems, so this may be the closest you get. – Ryan Morton Aug 21 '19 at 14:49
  • totally agree. Thank you very much for your help ! – Porkbun-Wei Aug 21 '19 at 16:25
  • Sure thing. Thanks for accepting the answer even if it wasn't exactly what you needed. Hopefully, it points you to a direction that is useful. Cheers and happy coding! – Ryan Morton Aug 21 '19 at 16:30