press Button “enter” key action in shiny Math.random()

Published by onesixx on

특정 페이지에서만 js를 실행하는 가장 좋은 방법
R
R

removeUI(selector = "button#logInsignin")

removeUI(selector = “button#logInsignin”)

R

#tags$head(tags$script(src = "../../www/AAA.js")),
tags$head(tags$script(HTML(
  "$(document).keyup(function(event) {
    if ($('#BB').is(':focus') && (event.keyCode == 13)) {
      $('#CC').click();
    }
  });"
))),

  	# tags$head(tags$script(src = "enter_button.js")),

  	  tags$head(tags$script(HTML(
  	  	"$(document).keyup(function(event) {
  	  		if ($('#logInpassword').is(':focus') && (event.keyCode == 13)) {
  	  			$('#logInsignin').click();
  	  		}
  	  	});"
  	  ))),

https://stackoverflow.com/questions/31415301/shiny-responds-to-enter

library(shiny)

ui <- fluidPage(
    ##############
    tags$script(
        '$(document).on("keyup", function(e) {
            if(e.keyCode == 13){Shiny.onInputChange("ENTERKeyPressed", Math.random());}
        });'
    ),
    #############
    textInput( "logInusername",    "Login",    placeholder = "Username"),
    passwordInput("logInpassword", "PASSWORD", placeholder = "Password"),
    actionButton( "logInsignin",   "Sign in"),br(),
    
    verbatimTextOutput("result")
)

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

    observeEvent({input$logInsignin 
                  input$ENTERKeyPressed}, {
        if((input$logInusername)!="") {
            output$result <- renderText({
                paste0("Welcome ", input$logInusername)
            })
        }else{
            print("Your username is incorrect - please try again.")
        }
    })
}

shinyApp(ui, server)

js <- '
$(document).on("keyup", function(e) {
if(e.keyCode == 13){
Shiny.onInputChange("keyPressed", Math.random());
}
});
'
library(shiny)

ui <- fluidPage(
    tags$script(js),
    textInput("title", label = "Title"),
    plotOutput("ggplot")
)
server <- function(input, output, session){
    Title <- reactiveVal()
    observeEvent(input[["keyPressed"]], {
        Title(input[["title"]])
    })
    output[["ggplot"]] <- renderPlot({
        ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) +
            geom_point() +
            ggtitle(Title())
    })
}
shinyApp(ui, server)

Categories: PjShiny

onesixx

Blog Owner

Leave a Reply

Your email address will not be published.