我正在寻找关于如何实际绑定JavaScript到Shiny(R仪表板)的信息来源,并解释实际发生了什么。
背景: 我完成了w3schools上的js和jquery教程,并学习了一些有关使用Scott Murray的书(交互式数据可视化)的d3的知识。我希望这足以让我理解有关如何在Shiny网站上构建自定义输入/输出绑定的示例和说明。
http://shiny.rstudio.com/articles/building-inputs.html
不幸的是,我没有找到任何最小工作代码示例。许多在GitHub上的示例对我来说过于复杂,可能是因为我对JavaScript的经验太少了。以下是一个使用JavaScript进行自定义输入绑定的示例:
https://github.com/jcheng5/shiny-js-examples/tree/master/input
这是一个我尝试展示的输入和输出绑定的示例:
<script src="http://d3js.org/d3.v3.js"></script>
<script type="text/javascript">
(function(){
// Probably not idiomatic javascript.
this.countValue=0;
// BEGIN: FUNCTION
updateView = function(message) {
var svg = d3.select(".d3io").select("svg")
svg.append("text")
.transition()
.attr("x",message[0])
.attr("y",message[1])
.text(countValue)
.each("end",function(){
if(countValue<100) {
countValue+=1;
$(".d3io").trigger("change");
}
})
}
// END: FUNCTION
//BEGIN: OUTPUT BINDING
var d3OutputBinding = new Shiny.OutputBinding();
$.extend(d3OutputBinding, {
find: function(scope) {
return $(scope).find(".d3io");
},
renderError: function(el,error) {
console.log("Foe");
},
renderValue: function(el,data) {
updateView(data);
console.log("Friend");
}
});
Shiny.outputBindings.register(d3OutputBinding);
//END: OUTPUT BINDING
//BEGIN: INPUT BINDING
var d3InputBinding = new Shiny.InputBinding();
$.extend(d3InputBinding, {
find: function(scope) {
return $(scope).find(".d3io");
},
getValue: function(el) {
return countValue;
},
subscribe: function(el, callback) {
$(el).on("change.d3InputBinding", function(e) {
callback();
});
}
});
Shiny.inputBindings.register(d3InputBinding);
//END: OUTPUT BINDING
})()
</script>
在ui中,"d3io"是一个div元素,updateView()是一个函数。以下是ui:
#UI
library(shiny)
d3IO <- function(inputoutputID) {
div(id=inputoutputID,class=inputoutputID,tag("svg","")) #; eerst zat ; erbij, maar werkt blijkbaar ook zonder
}
# Define UI for shiny d3 chatter application
shinyUI(pageWithSidebar(
# Application title
headerPanel("D3 Javascript chatter",
"Demo of how to create D3 I/O and cumulative data transfer"),
sidebarPanel(
tags$p("This widget is a demonstration of how to wire shiny direct to javascript, without any input elements."),
tags$p("Each time a transition ends, the client asks the server for another packet of information, and adds it
to the existing set"),
tags$p("I can't claim this is likely to be idiomatic javascript, because I'm a novice, but it allows d3 apps
to do progressive rendering. In real use, a more complex request/response protocol will probably be
required. -AlexBBrown")
),
mainPanel(
includeHTML("d3widget.js"),
d3IO("d3io") #Creates div element that d3 selects
)
))
这里是服务器文件:
# SERVER
library(shiny)
# Define server logic required to respond to d3 requests
shinyServer(function(input, output) {
# Generate a plot of the requested variable against mpg and only
# include outliers if requested
output$d3io <- reactive(function() {
if (is.null(input$d3io)) {
0;
} else {
list(rnorm(1)*400+200,rnorm(1)*400+200);
}
})
})
具体问题:
1)server.r 似乎接收名为 "d3io"(input$d3io)的输入,因为在 ui.r 中没有定义它,我推断它必须来自 javascript 文件。它实际上指的是哪个元素?
2)我对自定义绑定部分感到困惑:
var d3OutputBinding = new Shiny.OutputBinding();
$.extend(d3OutputBinding, {
find: function(scope) {
return $(scope).find(".d3io");
},
renderError: function(el,error) {
console.log("Foe");
},
renderValue: function(el,data) {
updateView(data);
console.log("Friend");
}
});
Shiny.outputBindings.register(d3OutputBinding);
我理解的是:
创建一个新的闪亮输出绑定,首先找到类.d3io(div元素),如果出错则写入控制台“Foe”(这是特殊代码吗?),如果没有错误则使用updateView函数使用数据渲染值(它从哪里接收该值?),并写入控制台“Friend”。最后注册输出。
希望你们能帮忙!我正在创建一份文档,介绍“当你不知道任何JavaScript时,学习如何将JavaScript实现到闪亮中所需的步骤”,我会很高兴的:)
谢谢, Long