首先,我对javascript及其库d3相当不熟悉.js,但我熟悉R.使用Shiny创建仪表盘既有趣又简单(多亏了stackoverflow).现在我想通过连接d3元素来扩展它.
我正在寻找关于如何将javascript绑定到Shiny(R dashboard)并解释实际情况的信息源.
背景:
http://shiny.rstudio.com/articles/building-inputs.html
但不幸的是,我没有,而且我似乎找不到任何在最小工作代码中的示例.github上的许多例子对我来说太复杂了,很可能是因为我对javascript的经验太少.下面是一个使用javascript自定义输入绑定的示例:
https://github.com/jcheng5/shiny-js-examples/tree/master/input
下面是一个输入&;我try 展开的输出绑定:
<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>
其中"d3io"是ui中的div元素,updateView()是一个函数.以下是用户界面:
#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) 服务器.由于"d3io"似乎未在"输入"中定义($d3io).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 using data呈现值(它从哪里接收该值?)写信安慰"朋友".最后注册输出.
希望你们能帮忙!我正在创建一个文档,其中包含"学习如何在不懂任何javascript的情况下将javascript实现到shiny中的必要步骤",我很乐意!)
干杯