使用clickAction = NULL将networkD3中的节点链接到网站 [英] linking a node in networkD3 to a website using clickAction = NULL

查看:124
本文介绍了使用clickAction = NULL将networkD3中的节点链接到网站的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用rnetworkD3软件包中的功能forceNetwork()将节点用作指向外部网站的链接?我在想也许要修改clickAction?

Is there a way to use a node as a link to an external website using the function forceNetwork() in the networkD3 package in r? I was thinking maybe modifying the clickAction?

示例数据:

library(networkD3)
data(MisLinks)
data(MisNodes)

# Create a random URL in the nodes dataset
MisNodes$URL <- paste0("http://www.RANDOMLINK_", sample(1:100, NROW(MisNodes)), ".com")
head(MisNodes)

MyClickScript <- 'alert(d.index)'

forceNetwork(Links = MisLinks, Nodes = MisNodes,
             Source = "source", Target = "target",
             Value = "value", NodeID = "name",
             Group = "group", opacity = 0.8,
             clickAction = MyClickScript)

所需结果:当用户单击某个节点时,将打开一个新标签(例如window.open),指向该节点的关联URL-如何获得clickAction指向MisNodes$URL[d.index]?

Desired outcome: When a user clicks on a node, a new tab will open (e.g. window.open) pointing to the associated URL for the node - How can I get clickAction to point to MisNodes$URL[d.index]?

推荐答案

networkD3设计并不容易.这是一种答案.我将尝试内联注释以解释我们在每个步骤中所做的事情.

networkD3 design does not make this easy. Here is one way to answer. I'll try to comment inline to explain what we are doing in each step.

library(networkD3)

# example from ?forceNetwork
data(MisLinks)
data(MisNodes)
# Create graph
fn <- forceNetwork(
  Links = MisLinks, Nodes = MisNodes, Source = "source",
  Target = "target", Value = "value", NodeID = "name",
  Group = "group", opacity = 0.4, zoom = TRUE
)

# let's look at our forceNetwork
#   nodes are provided to JavaScript
#   in a nodes data.frame
str(fn$x$nodes)

# make up some links to demonstrate
#   how we can add them to our nodes df
fn$x$nodes$hyperlink <- paste0(
  'http://en.wikipedia.org/wiki/Special:Search?search=',
  MisNodes$name
)

# then with our hyperlinks in our data
#   we can define a click action to open
#   the hyperlink for each node in a new window
fn$x$options$clickAction = 'window.open(d.hyperlink)'

fn

这篇关于使用clickAction = NULL将networkD3中的节点链接到网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆