1
我期待通過按方格div /按鈕(頁面上的第三個元素)隨機數字將出現在元素下,元素的顏色也將改變。不幸的是,我似乎必須按下頁面頂部的按鈕才能發生這種情況,我無法弄清楚爲什麼?發送數據到閃亮的服務器,並返回發生延遲
我的index.html
<html>
<head>
<script type="text/javascript" src="shared/jquery.js"></script>
<script type="text/javascript" src="shared/shiny.js"></script>
</head>
<body>
<input id="button" type="submit" name="submit" value="Press me!" class="action-button">
<p><div id="text" class="shiny-text-output" style="width: 150px; height: 25px; border: 1px solid black;"></div>
<p><div id="mydiv" style="width: 50px; height: 50px;"></div>
<pre id="results" class="shiny-text-output"></pre>
</body>
<script type="text/javascript">
// send data to shiny server
document.getElementById ("mydiv").onclick = function() {
var number = Math.random();
Shiny.onInputChange ("mydata", number);
};
// handlers to receive data from server
Shiny.addCustomMessageHandler ("myColorHandler",
function (color) {
document.getElementById ("mydiv").style.backgroundColor = color;
}
);
</script>
</html>
我server.R
shinyServer (function (input, output, session)
{
observe ({
x = input$button;
output$text = renderText ({
if (! is.null (x))
{
if (x %% 2 == 0) { paste (x, ": you are even", sep=""); }
else { paste (x, ": you are odd", sep=""); }
}
})
});
output$results = renderPrint ({
input$mydata;
})
observe ({
input$mydata;
color = rgb (runif (1), runif (1), runif (1));
session$sendCustomMessage (type="myColorHandler", color);
});
})
非常感謝。 – gaitat
樂意幫忙... – jdharrison