我在Shiny App中编写了自定义的sliderinput,用于我计划的研究。我在跨域iframe中加入了Shiny App。
现在,我想让Shiny执行我的JavaScript,该JavaScript在我创建的测试网站上可以正常运行。有人可以帮我实现吗?
理想情况下,我想取消按钮并将sliderinput发送到父框架。
代码示例Shiny UI
library(shiny)fluidPage( wellPanel(includeScript(path="index2.js")),
verticalLayout(
titlePanel("Vertical layout example"),
plotOutput("plot1"),
wellPanel(
sliderInput("n", "Number of points", 10, 200,
value = 50, step = 10)
)
))
代码示例Shiny Server
library(shiny) function(input, output,session) {
output$plot1 <- renderPlot({
plot(x = rnorm(input$n), y = rnorm(input$n))
})}
JavaScript代码
<p>Send Message: <button id="message_button">Hi parent</button></p>
<script>
function bindEvent(element, eventName, eventHandler) {
if (element.addEventListener) {
element.addEventListener(eventName, eventHandler, false);
} else if (element.attachEvent) {
element.attachEvent('on' + eventName, eventHandler);
}
}
var sendMessage = function (msg) {
// Make sure you are sending a string, and to stringify JSON
window.parent.postMessage(msg, '*');
};
var results = document.getElementById('results'),
messageButton = document.getElementById('message_button');
bindEvent(messageButton, 'click', function (e) {
sendMessage("Hi");
});
</script>