代码之家  ›  专栏  ›  技术社区  ›  CodeNoob

R networkD3:单击action以显示来自节点数据帧的信息

  •  2
  • CodeNoob  · 技术社区  · 6 年前


    library(networkD3)
    
    # Load data
    data(MisLinks)
    data(MisNodes)
    
    new.nodes <- MisNodes
    new.nodes$var1 <- runif(nrow(MisNodes),1,2)
    new.nodes$var2 <- runif(nrow(MisNodes),1,2)
    
    # Some script to show the node index in the new.nodes data frame
    script <- 'alert("row: " + (d.index + 1));'
    
    # Plot
    forceNetwork(Links = MisLinks, Nodes = new.nodes,
                 Source = "source", Target = "target",
                 Value = "value", NodeID = "name",
                 Group = "group", opacity = 0.8,
                 clickAction = script)
    

    d.index + 1

        name group size     var1     var2
    1 Myriel     1   15 1.501311 1.053062
    

    2 回复  |  直到 6 年前
        1
  •  4
  •   CJ Yetman    6 年前

    forceNetwork 函数去掉了传递给JavaScript的数据帧中所有不必要的变量/列,这通常是有利的,但在本例中不是。您可以通过保存 力网络 创建,然后添加回需要访问的变量,然后“打印”/显示它。

    library(networkD3)
    
    data(MisLinks)
    data(MisNodes)
    
    MisNodes$var1 <- runif(nrow(MisNodes),1,2)
    
    script <- 'alert("var1: " + (d.var1));'
    
    fn <- forceNetwork(Links = MisLinks, Nodes = new.nodes, Source = "source", 
                       Target = "target", Value = "value", NodeID = "name", 
                       Group = "group", opacity = 0.8, clickAction = script)
    
    fn$x$nodes$var1 <- MisNodes$var1
    
    fn
    

    enter image description here


    对于多个变量也可以这样做,例如。。。

    library(networkD3)
    
    data(MisLinks)
    data(MisNodes)
    
    MisNodes$var1 <- runif(nrow(MisNodes),1,2)
    MisNodes$var2 <- runif(nrow(MisNodes),1,2)
    
    script <- 'alert("var1: " + d.var1 + "\\n" + "var2: " + d.var2);'
    
    fn <- forceNetwork(Links = MisLinks, Nodes = new.nodes, Source = "source", 
                       Target = "target", Value = "value", NodeID = "name", 
                       Group = "group", opacity = 0.8, clickAction = script)
    
    fn$x$nodes$var1 <- MisNodes$var1
    fn$x$nodes$var2 <- MisNodes$var2
    
    fn
    
        2
  •  1
  •   Anders Ellern Bilgrau    6 年前

    不幸的是,如果不是不可能的话,我认为这是不容易做到的。似乎只有 forceNetwork

    script <- 'alert("row: "     + (d.index + 1) + 
                     ", name: "  + d.name + 
                     ", group: " + d.group + 
                     ", size: "  + d.nodesize + 
                     ", var1: "  + d.var1)'
    
    # Plot
    d3 <- forceNetwork(Links = MisLinks, Nodes = new.nodes,
                 Source = "source", Target = "target",
                 Value = "value", NodeID = "name", 
                 Nodesize = "size",
                 Group = "group", opacity = 0.8,
                 clickAction = script)
    
    print(d3)
    

    正如您在单击下面的节点时所看到的,var1是未定义的。注意我添加了 Nodesize d.nodesize (而不是 d.size group 在本例中,变量和参数的名称是相同的,因此这里没有区别。然而 NodeID 显然是通过我发现不一致的指定名称访问的。

    enter image description here

    您可以通过保存以下内容来查看生成的源:

    saveNetwork(d3, file = "test.html")
    

    通过查看它,可以注意到要输出的值没有存储在文件中。

    编辑:

    根据您自己的建议,这是一个解决方案:

    new.nodes$newname  <- with(new.nodes, paste(", name: ", name, ", group: ", group, 
                                    ", size: ", size, ", var1: ", var1,
                                    ", var2: ", var2))
    
    newscript <- 'alert("row: " + (d.index + 1) + d.name)'
    
    forceNetwork(Links = MisLinks, Nodes = new.nodes,
                 Source = "source", Target = "target",
                 Value = "value", NodeID = "newname", Nodesize = "size",
                 Group = "group", opacity = 0.8, opacityNoHover = 0,
                 clickAction = newscript)
    

    enter image description here

    所以,这只是一个格式化字符串的问题,因为你希望它显示。缺点是name/nodeid很长。正如你所看到的,NodeID总是被称为 name 在JS方面。