How to use shiny javascript functions?

后端 未结 2 1696
终归单人心
终归单人心 2021-02-06 07:27

I want to send data from javascript to R using shiny js function, but is not working. What I have done is a simple example, in which setinputValue send \"noone\" to \"too\" inpu

相关标签:
2条回答
  • 2021-02-06 08:07

    I just wanted to point out that the function being called in the answer by shosaco differs from original post which isn't that clear from the reasons why it didn't work.

    Shiny.setInputValue('too', 'noone');
    

    to

    Shiny.onInputChange("too", "noone");           
    
    0 讨论(0)
  • 2021-02-06 08:10

    It does not work with $( document ).ready(function(), but with $( document ).on("shiny:sessioninitialized", function(event) {:

    library(shiny)  
    ui <- fluidPage(      
      HTML('<script>
           $( document ).on("shiny:sessioninitialized", function(event) {
               Shiny.onInputChange("too", "noone");           
           });</script>'),         
      textOutput("table")      
    )
    
    server <- function(input, output) {
      output$table <- renderPrint(input$too) 
    }
    
    shinyApp(ui,server)
    

    Reason for this is given in the tutorial: You cannot call the function too soon, you need a little time until Shiny is ready to update the input value:

    in message.js, we wrapped our code in $(document).ready(function() { ... }. This jQuery function will tell the browser to only run the code inside, once the page, i.e. the Document Object Model (DOM), is ready for JavaScript code to execute. Note that when we activate this code too soon, i.e. before the image is loaded, we cannot yet attach an event handler to it. In other words, here we want to be sure that the image exists before attaching an event handler to it. ```

    0 讨论(0)
提交回复
热议问题