Jquery D3.js树在单击时传递node.name

Jquery D3.js树在单击时传递node.name,jquery,r,node.js,d3.js,shiny,Jquery,R,Node.js,D3.js,Shiny,我想首先谈谈我的意图 1) 在中创建预定义的D3.js可折叠树 2) 单击某个节点时,该特定节点的名称将从D3.js传递到R,以进行进一步的操作 现在,我有了一个预定义的D3.js可折叠树。我在下面提供了js、server.R和ui.R的代码。现在,我只有一个交互式图形,但我想再进一步 只要我点击一个节点,我就想把节点的名称作为一个变量 我做了研究(主要在这里:),所以我知道我必须使用d3OutputBinding来创建一个新变量。我进一步发现 .on('click', function(no

我想首先谈谈我的意图

1) 在中创建预定义的D3.js可折叠树

2) 单击某个节点时,该特定节点的名称将从D3.js传递到R,以进行进一步的操作

现在,我有了一个预定义的D3.js可折叠树。我在下面提供了js、server.R和ui.R的代码。现在,我只有一个交互式图形,但我想再进一步

只要我点击一个节点,我就想把节点的名称作为一个变量

我做了研究(主要在这里:),所以我知道我必须使用d3OutputBinding来创建一个新变量。我进一步发现

 .on('click', function(node) {
          alert(node.name);
将至少弹出一条带有节点名称的消息。所以我想,我必须使用

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
并通过反应式函数(输入$.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
)
库(ShinydaShashboard)
图书馆(shinyjs)

侧边栏我认为您不需要闪亮的
OutputBinding()对于这一部分,您只需使用
shinny.onInputChange
将节点名称传递回
server.R

我没有运行您的全部代码,但您可以尝试:

.on('click', function(node) {
          Shiny.onInputChange("node_name", node.name);
}
server.R
中,
input$node\u name
将保存节点的名称

编辑

要保持折叠树,只需编辑javascript末尾的
单击
函数:

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);
}
  });

在尝试复制时:当我c&p您的js代码时,我在第72行、第152行和第162行得到错误。您打算关闭第72行的消息处理程序吗?太好了!此外,还可以创建多个变量,如parent.name并传递它们。很抱歉,现在我遇到了另一个问题:由于我已删除了
。on('click',click)
我的树不再可折叠:(.只需将其添加到
函数(节点)
中,没有任何帮助。我已尝试添加另一个
。on('click',click)
但这并没有完成javascript末尾的技巧,有一个名为
函数单击(d)
的函数。将该函数中的代码粘贴到新函数中。确保您的替换
d
也由
节点进行。或者直接将闪亮的.onInputChange添加到该函数中
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);
}
  });