我想首先表达我的意图
1)在Shiny中创建预定义的D3.js可折叠树
2) 单击 Node 时,该特定 Node 的名称将从 D3.js 传递到 R 以进行进一步操作。
好吧,此时我在 Shiny 中有一个预定义的 D3.js 可折叠树。我在下面提供了 js、server.R 和 ui.R 的代码。现在,我只有一个交互式图形,但我想更进一步。
一旦我点击一个 Node ,我就想获取该 Node 的名称作为变量。
我做了我的研究(主要在这里: https://github.com/metrumresearchgroup/SearchTree ),所以我知道,我必须使用 d3OutputBinding 来创建一个新变量。我进一步发现,
.on('click', function(node) {
alert(node.name);
至少会弹出一条包含 Node 名称的消息。所以我想,我必须使用类似
var d3OutputBinding = new Shiny.OutputBinding();
$.extend(d3OutputBinding, {
find: function(scope) {
return $(scope).find('.div_tree2');
},
renderValue: function(el) {
var svg = d3.select(el).select("svg");
...
function update(source) {
...
nodeEnter = node.enter().append("g")
.on('click', function(node) {var nodes1 = node.name; });
...
Shiny.onInputChange(".nodesData", JSON.decycle(nodes1));
...
} // end of renderValue
}); // end of .extend
并通过 react 函数(input$.nodesData)将其插入server.R。
不幸的是,我太笨了,无法实现这一点。所以我恳请您提供任何建议。
如前所述,我将提供我的代码。这是我的 d3script_tree.js
Shiny.addCustomMessageHandler("jsondata_tree",
function(message){
var treeData = [
{
"name": "Parent",
"parent": "null",
"children": [
{
"name": "A",
"parent": "Parent"
},
{
"name": "B",
"parent": "Parent"
}
]
}
];
// ************** Generate the tree diagram *****************
var margin = {top: 20, right: 120, bottom: 20, left: 120},
width = 960 - margin.right - margin.left,
height = 250 - margin.top - margin.bottom;
var i = 0,
duration = 750,
root;
var tree = d3.layout.tree()
.size([height, width]);
var diagonal = d3.svg.diagonal()
.projection(function(d) { return [d.y, d.x]; });
var svg = d3.select("#div_tree2").append("svg")
.attr("width", width + margin.right + margin.left)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
root = treeData[0];
root.x0 = height / 2;
root.y0 = 0;
update(root);
d3.select(self.frameElement).style("height", "500px");
function update(source) {
// Compute the new tree layout.
var nodes = tree.nodes(root).reverse(),
links = tree.links(nodes);
// Normalize for fixed-depth.
nodes.forEach(function(d) { d.y = d.depth * 180; });
// Update the nodes…
/* var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); })
*/
var node = svg.selectAll("g.node")
.data(nodes, function(d) { return d.id || (d.id = ++i); })
// Enter any new nodes at the parent's previous position.
var nodeEnter = node.enter().append("g")
.attr("class", "node")
.attr("transform", function(d) { return "translate(" + source.y0 + "," + source.x0 + ")"; })
//.on("click", click);
.on('click', function(node) {
alert(node.name);
});
nodeEnter.append("circle")
.attr("r", 1e-6)
.style("fill", function (d) { return '#05415A'; })
//.style("fill", function(d) { return d._children ? "#C00000" : "#fff"; });
nodeEnter.append("link")
.style("stroke-width", "3px");
nodeEnter.append("text")
.attr("x", function(d) { return d.children || d._children ? -13 : 13; })
.attr("dy", ".35em")
.attr("text-anchor", function(d) { return d.children || d._children ? "end" : "start"; })
.text(function(d) { return d.name; })
.style("fill-opacity", 1e-6);
// Transition nodes to their new position.
var nodeUpdate = node.transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + d.y + "," + d.x + ")"; });
nodeUpdate.select("circle")
.attr("r", 10)
.style("fill", function (d) { return '#05415A'; })
// .style("fill", function(d) { return d._children ? "#C00000" : "#fff"; });
nodeUpdate.select("text")
.style("fill-opacity", 1);
nodeUpdate.select("link")
.style("stroke-width", "3px");
// Transition exiting nodes to the parent's new position.
var nodeExit = node.exit().transition()
.duration(duration)
.attr("transform", function(d) { return "translate(" + source.y + "," + source.x + ")"; })
.remove();
nodeExit.select("circle")
.attr("r", 1e-6);
nodeExit.select("text")
.style("fill-opacity", 1e-6);
// Update the links…
var link = svg.selectAll("path.link")
.data(links, function(d) { return d.target.id; });
// Enter any new links at the parent's previous position.
link.enter().insert("path", "g")
.attr("class", "link")
.attr("d", function(d) {
var o = {x: source.x0, y: source.y0};
return diagonal({source: o, target: o});
});
// Transition links to their new position.
link.transition()
.duration(duration)
.attr("d", diagonal);
// Transition exiting nodes to the parent's new position.
link.exit().transition()
.duration(duration)
.attr("d", function(d) {
var o = {x: source.x, y: source.y};
return diagonal({source: o, target: o});
})
.remove();
// Stash the old positions for transition.
nodes.forEach(function(d) {
d.x0 = d.x;
d.y0 = d.y;
});
}
// Toggle children on click.
function click(d) {
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
});
接下来我有 ui.R 引用它。 CSS 样式并不重要,因此可以忽略。
library(shinydashboard)
library(shinyjs)
sidebar <- dashboardSidebar(
sidebarMenu(
menuItem("Toy example", tabName = "toy", icon = icon("cog"))
)
)
body <- dashboardBody(
tags$head(
tags$link(rel = "stylesheet", type = "text/css", href = "custom.css")
),
tabItems(
tabItem(tabName = "toy",
tags$div(class="d3-div",
#to style to d3 output pull in css
tags$head(tags$link(rel = "stylesheet", type = "text/css", href = "style.css")),
#load D3JS library
tags$script(src="d3.min.js"),
#tags$script(src="https://d3js.org/d3.v3.min.js"),
#load javascript
tags$script(src="d3script_tree.js"),
#create div referring to div in the d3script
tags$div(id="div_tree2"))
)
)
)
# Put them together into a dashboardPage
dashboardPage(
dashboardHeader(title = "Toy Example"),
sidebar,
body
)
最后,有 server.R 来触发这个 Java 脚本。
library(shiny)
library(shinyjs)
shinyServer(function(input, output, session) {
options(shiny.maxRequestSize=30*1024^2, shiny.usecairo=T)
var_json_tree <- toJSON(1, pretty = T)
#push data into d3script
session$sendCustomMessage(type="jsondata_tree",var_json_tree)
})
最佳答案
我认为您不需要 Shiny.OutputBinding();
部分,您只需使用 Shiny.onInputChange
将 Node 名称传递回 server.R
.
我没有运行你的整个代码,但你可以尝试:
.on('click', function(node) {
Shiny.onInputChange("node_name", node.name);
}
在server.R
中,input$node_name
将保存 Node 的名称。
编辑
要保持折叠树,您只需编辑 JavaScript 末尾的 click
函数即可:
function click(d) {
Shiny.onInputChange("node_name", d.name);
if (d.children) {
d._children = d.children;
d.children = null;
} else {
d.children = d._children;
d._children = null;
}
update(d);
}
});
关于jquery - D3.js 树在点击时将 node.name 传递给 R Shiny,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42237617/