-2

Bonjour!

I wonder how to "easily" implemant the Konami code in any R shiny app

INPUT (from user keyboard on the R shiny app page)
Arrow UP, UP, DOWN, DOWN , LEFT, RIGHT, LEFT, RIGHT, B, A

OUTPUT
- Add a fun image on the dashboard
(- Change color of the app)

S.Br
  • 33
  • 4
  • 1
    Something like this: https://stackoverflow.com/questions/24973549/r-shiny-key-input-binding ; or this: https://stackoverflow.com/questions/41675059/keyboard-shortcuts-to-trigger-reactive-flows-in-r-shiny ? – SebSta Mar 04 '20 at 12:04
  • yeah maybe but my knowledge of HTML and JS is very limited – S.Br Mar 04 '20 at 12:18
  • Maybe someone could show me a basic code of shiny app which display an image on the dashboard if the user enter the konami code.. Man, I am making a shiny app and so many hours into it that I definitively want to add a joke like this – S.Br Mar 04 '20 at 15:11
  • Does this answer your question? [R Shiny key input binding](https://stackoverflow.com/questions/24973549/r-shiny-key-input-binding) – Brian Mar 04 '20 at 15:48

1 Answers1

0
library(shiny)
runApp( list(ui = bootstrapPage(
  verbatimTextOutput("results"),
  verbatimTextOutput("allInputs"),
  tags$script('
              $(document).on("keypress", function (e) {
              Shiny.onInputChange("mydata", e.which);
              });
              ') 
)
, server = function(input, output, session) {

  output$results = renderPrint({
    input$mydata
  })

  keysPressed <- reactiveVal()

  observeEvent(input$mydata, {
    keysPressed(c(keysPressed(), input$mydata))
  })

  output$allInputs = renderPrint({
    keysPressed()
  })

}
))

ismirsehregal who helped me on the code I found
Is there a way in R shiny to concatenate all values taken by an input

My final code merging all hints:

UI.R (inside body)

          tags$script('
                   pressedKeyCount = 0;
                      $(document).on("keydown", function (e) {
                      Shiny.onInputChange("pressedKey", pressedKeyCount++);
                      Shiny.onInputChange("pressedKeyId", e.which);
                      });'
            ),

SERVER.R (somewhere)

keysPressed <- reactiveVal()

observeEvent(input$pressedKey, {
  keysPressed(c(keysPressed(), input$pressedKeyId))
})

output$secret <- renderUI({
  secretCode = keysPressed()
  if(length(secretCode)>=10)
  secretCode = secretCode[(length(secretCode)-9):length(secretCode)]
  trueCode = c(38,38,40,40,37,39,37,39,66,65)
  if(isTRUE(all.equal(secretCode,trueCode))){
    HTML('<center><img src="4BA140A6A75546D791B01BC0BA9E00A2.png"></center>')
    }
})

c(38,38,40,40,37,39,37,39,66,65) corresponds to famous konami code sequential key inputs

S.Br
  • 33
  • 4