सबसे पहले, मैं जावास्क्रिप्ट और इसकी लाइब्रेरी d3.js से काफी अपरिचित हूं, लेकिन मैं आर से परिचित हूं। चमकदार का उपयोग करके डैशबोर्ड बनाना मजेदार और आसान है (स्टैक ओवरफ्लो के लिए धन्यवाद)। अब मैं इसे डी 3 तत्वों से कनेक्ट करके विस्तारित करना चाहता हूं।बाध्यकारी जावास्क्रिप्ट (d3.js) चमकदार
मैं वास्तव में चमकदार (आर डैशबोर्ड) में जावास्क्रिप्ट को बाध्य करने और वास्तव में क्या चल रहा है, इस बारे में जानकारी स्रोतों की तलाश में हूं।
पृष्ठभूमि: मैंने w3schools पर जेएस और jquery पर ट्यूटोरियल किया और स्कॉट मरे की पुस्तक (वेब के लिए इंटरएक्टिव डेटा विज़ुअलाइज़ेशन) का उपयोग करके डी 3 के बारे में सीखा (थोड़ा)। मैं आशा व्यक्त की यह मेरे उदाहरण और कस्टम इनपुट/आउटपुट बाइंडिंग चमकदार वेबसाइट पर निर्माण करने के लिए कैसे के विषय में स्पष्टीकरण को समझने बनाने के लिए पर्याप्त होगा:
http://shiny.rstudio.com/articles/building-inputs.html
लेकिन दुर्भाग्य से मैं नहीं करते हैं और मैं नहीं कर पा रहे कम से कम काम करने वाले कोड में कोई उदाहरण खोजें। जिथब पर कई उदाहरण मेरे लिए विच्छेदन करने के लिए जटिल हैं, संभवतः जावास्क्रिप्ट के साथ मेरे छोटे अनुभव के कारण। यहाँ कस्टम इनपुट का एक उदाहरण जावास्क्रिप्ट के साथ बाध्यकारी है:
<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" कहाँ एक div तत्व है:
https://github.com/jcheng5/shiny-js-examples/tree/master/input
यहाँ बाध्यकारी मैं प्रकट करने की कोशिश एक इनपुट & उत्पादन का एक उदाहरण है ui में, 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);
}
})
})
विशिष्ट सवाल: यहाँ ui है
1) server.r "d3io" कहा जाता है इनपुट प्राप्त करने के लिए लगता है (इनपुट $ d3io) चूंकि यह ui.r में परिभाषित नहीं है, इसलिए मैंने तर्क दिया कि यह जावास्क्रिप्ट फ़ाइल से आना चाहिए। वास्तव में इसका कौन सा तत्व संदर्भित करता है?
2) मैं मुसीबत कस्टम बंधन हिस्सा समझ है:,
एक नया चमकदार outputbinding बनाएं, प्रथम श्रेणी .d3io (div तत्व) पाते हैं तो त्रुटि:
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);
मेरे समझ है कंसोल "फो" (क्या यह विशेष कोड है?), यदि त्रुटि नहीं है तो फ़ंक्शन अपडेट का उपयोग करके वैल्यू देखें डेटा का उपयोग करके देखें (यह मान कहां से प्राप्त होता है?) और कंसोल "मित्र" को लिखें। अंत में आउटपुट रजिस्टर करें।
आशा है कि आप लोग मदद कर सकते हैं! मैं "कैसे चमकदार में जावास्क्रिप्ट लागू करने के लिए जब आप किसी भी जावास्क्रिप्ट पता नहीं है जानने के लिए आवश्यक कदम", मुझे लगता है कि प्यार होता पर चरणों के साथ एक दस्तावेज़ बनाने रहा हूँ! :)
चीयर्स, लांग
+1 पर – Vincent
आप अभी भी कस्टम बाइंडिंग सहित लिखने के लिए एक गाइड में रुचि रखते हैं "आवश्यक कदम कैसे चमकदार में जावास्क्रिप्ट लागू करने के लिए जब आप नहीं जानते कि किसी भी जावास्क्रिप्ट जानने के लिए"। d3.js? – BigDataScientist