简体   繁体   中英

How can I call R shiny html file via javascript?

I am trying to make a very simple password protection for an R shiny web app file. The code you see below is contained in an index.html. Within that same folder is a file named "testflex.html". That is the file i want to protect with a password. Nothing happens when I enter the username and password. However, when I enter WRONG username or password the error message is displayed.

Any hints? (Code is below)

    function showPass(form){

        var user = form.username.value;
        var pass = form.pwd.value;

        var user1 = "admin" // this is the username
        var pass1 = "abcd1234" // this is the password

        if(user === user1 && pass === pass1){

            window.open = "testflex.html";

        }

        else{
            document.write("Wrong password or username");
        }
    }
</script>

<body>

  <form>
    Username: <input type="text" name="username" /> <br />
    Password: <input type="password" name="pwd" /> <br />
      <input type="button" value="Log In" onclick="showPass(form)" />   </form> </body>

Interesting idea. Concerning your question: Use window.open("testflex.html") instead of window.open = "testflex.html"; This works for me:

library(shiny)

openWindow <- '
Shiny.addCustomMessageHandler("resetValue", function(message) {
  window.open("new.html");
});
'

# Define UI for application that draws a histogram
ui <- shinyUI(fluidPage(

  sidebarLayout(
    sidebarPanel(
      tags$head(tags$script(HTML(openWindow))),
      selectInput("open", "Class Type:", c(FALSE, TRUE))
    ),

    mainPanel(
      textOutput("class")
    )
  )
))

server <- shinyServer(function(input, output, session) {
  global <- reactiveValues(sample = 1:9)

  observe({
    if(input$open){
      session$sendCustomMessage(type = "resetValue", message = "keyVal")
    }
  })

  output$class <- renderText({
    print(input$open)
  })
})

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM