简体   繁体   English

为什么R Shiny不接受jQuery设置的文本输入?

[英]Why doesn't R Shiny accept text input set by jQuery?

I am trying to build an R Shiny app without a ui.R file and nearly got it working but stumbled over the final hurdle. 我正在尝试构建一个没有ui.R文件的R Shiny应用程序,几乎让它正常工作,但偶然发现了最后的障碍。 Below is a simplest working demonstration of the problem: 以下是该问题的最简单的工作演示:

  1. The index.html file defines two buttons, one named 'RAND' which is controlled by jQuery, and a second named 'ADD1', which is controlled by R Shiny. index.html文件定义了两个按钮,一个名为'RAND',由jQuery控制,另一个名为'ADD1',由R Shiny控制。 I also have two textboxes named 'temp' and 'out'. 我还有两个名为'temp'和'out'的文本框。
  2. The goal of this dummy program is for 'RAND' to load a random number into 'temp', and for 'ADD1' to then increment the value stored in 'temp' by 1. 这个虚拟程序的目标是'RAND'将随机数加载到'temp'中,而'ADD1'则将'temp'中存储的值加1。
  3. The 'RAND' button works fine, but the 'ADD1' button produces 'NA'. 'RAND'按钮工作正常,但'ADD1'按钮产生'NA'。 The funny thing is that if I modify the 'temp' value by hand and then click 'ADD1' again, it does produce the correct value! 有趣的是,如果我手动修改'temp'值然后再次点击'ADD1',它会产生正确的值!
  4. My questions are: why does the program behave as it does? 我的问题是:为什么程序的行为与它一样? And how can I make 'ADD1' work without having to manually retype the contents of 'temp'? 如何在不必手动重新键入'temp'的内容的情况下使'ADD1'工作?

Here is my index.html: 这是我的index.html:

<html>
<head>
<link rel="stylesheet" type="text/css" href="jquery-ui.css"/>
<script src="jquery.js" type="text/javascript"></script>
<script src="jquery-ui.js" type="text/javascript"></script>
<script src="test.js" type="text/javascript"></script>
<script src="shiny.js" type="text/javascript"></script>
</head>
<body>
<div id="RAND" name="RAND">RAND</div>
<div id="ADD1" name="ADD1" class="action-button">+1</div>
<p></p>
<div class="form-group shiny-input-container">
<input id="temp" name="temp" type="text" class="form-control"/>
</div>
<p></p>
<div id="out" class="shiny-text-output"></div>
</body>
</html>

This is server.R file: 这是server.R文件:

library(shiny)
shinyServer(function(input,output,session){ 
    observeEvent(input$ADD1, {
        output$out <- renderText({ as.numeric(input$temp) + 1 })
    });
});

And finally, this is my Javascript code (test.js): 最后,这是我的Javascript代码(test.js):

$(function(){
    $("#RAND").button()
    $("#RAND").click(function() {
        $("#temp").val(Math.random());
    });
    $("#ADD1").button()
});

Thanks to warmoverflow 's suggestion, I managed to answer my own question: 感谢warmoverflow的建议,我设法回答了我自己的问题:

index.html remains unchanged index.html保持不变

server.R becomes: server.R成为:

library(shiny)
shinyServer(function(input,output,session){ 
    observe({
        input$randval
    })
    observeEvent(input$ADD1, {
        output$out <- renderText({ as.numeric(input$randval) + 1 })
    })
})

and test.js becomes: 和test.js变成:

$(function(){
    $("#RAND").button()
    $("#RAND").click(function() {
        val = Math.random();
        $("#temp").val(val);
        Shiny.onInputChange("randval",val);
    });
    $("#ADD1").button()
});

Note that the value stored in $(#temp) is not really used for anything and could be removed. 请注意,存储在$(#temp)中的值并不真正用于任何事情,可以删除。 I just left it in the code for comparison with my initial formulation of the problem. 我把它留在代码中,以便与我最初的问题表述进行比较。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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