R shiny:在 Shiny App 中添加 Konami 代码

R shiny: Add Konami Code inside a Shiny App

您好!

我想知道如何 "easily" 在任何 R shiny 应用程序中实施 Konami 代码

INPUT(来自 R shiny 应用程序页面上的用户键盘)
箭头向上、向上、向下、向下、向左、向右、向左、向右、B、A

输出
- 在仪表板上添加一个有趣的图像
(- 更改应用程序的颜色)

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

我合并所有提示的最终代码:

UI.R(正文内)

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

SERVER.R(某处)

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) 对应着名的 konami 代码顺序键输入