有没有一种方法可以使用节点通过r中的networkD3包中的forceNetwork()函数将其用作到外部网站的链接,但已合并到闪亮的应用程序中?

我找到了这个答案here

但是当我尝试将其作为闪亮的应用程序运行时,它不起作用。
这是我的代码:

library(shiny)
library(networkD3)

source =c(0,0,3,3,3,7,7,10,9,7,1,6,4,5,8,2)
target = c(1,2,4,5,6,8,9,11,12,10,9,10,8,9,11,8)
value = c(10,10,10,10,10,10,10,20,20,10,2,2,2,10,20,15)
MisLinks = data.frame(source,target,value)


name = c("[Category]Genre", "CCG", "Action",  [Category]Art","Realistic Art", "Dark Art", "Cartoony", "[Category]Time  demend", "Mid-Core", "Hard-Core", "Casual", "Install", "Not Install")
group = c(1,2,2,3,4,4,4,5,6,6,6,8,8)
size = c(50,20,20,50,20,20,20,50,20,20,20,250,250)
hyperlink = c("http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://yahoo.com")
MisNodes = data.frame(name, group, size, hyperlink)



ui = fluidPage(
titlePanel("Testing app"),

sidebarLayout(
sidebarPanel(
  sliderInput("opacity", "Test", 0.6, min = 0.1, max = 1, step = .1)
),
mainPanel(
  tabsetPanel(
    tabPanel("Force Network", forceNetworkOutput("force"))
  )
)
)
)

Myclickaction = "window.open(d.name, '_blank')"

server = function(input,output) {

output$force = renderForceNetwork({
forceNetwork(Links = MisLinks, Nodes = MisNodes,
             Source = "source", Target = "target", charge = -150,
             legend = TRUE, opacityNoHover = 1, Nodesize = "size",
             Value = "value", NodeID = "name",
             Group = "group", linkWidth = 2, clickAction =  Myclickaction,
             opacity = 0.9, colourScale =JS("d3.scaleOrdinal(d3.schemeCategory20);"),
             zoom=t)


})
}
shinyApp(ui = ui, server = server)


预期的结果:在发布的闪亮应用程序中单击节点时
您会在浏览器中打开一个新窗口,该窗口取决于所单击的节点!

最佳答案

您在第10行的[Category]Art之前缺少引号
您在第44行传递了zoom=t而不是zoom=T
Myclickaction = "window.open(d.name, '_blank')"应该是第33行上的Myclickaction = "window.open(d.hyperlink, '_blank')"
在第38行上,将forceNetwork的输出保存到一个对象,以便以后可以对其进行修改,例如fn <- forceNetwork(Links = MisLinks, Nodes = MisNodes,
forceNetwork调用之后(第45行),添加fn$x$nodes$hyperlink <- hyperlink,然后在下一行添加fn


问题在于,运行forceNetwork函数之后需要添加超链接,因为此技巧不受官方支持,并且forceNetwork函数将删除节点对象中的超链接。

因此,带有规定的更改的完整代码以及一些格式修复程序看起来像...

library(shiny)
library(networkD3)

source =c(0,0,3,3,3,7,7,10,9,7,1,6,4,5,8,2)
target = c(1,2,4,5,6,8,9,11,12,10,9,10,8,9,11,8)
value = c(10,10,10,10,10,10,10,20,20,10,2,2,2,10,20,15)
MisLinks = data.frame(source,target,value)

name = c("[Category]Genre", "CCG", "Action",  "[Category]Art","Realistic Art", "Dark Art", "Cartoony", "[Category]Time  demend", "Mid-Core", "Hard-Core", "Casual", "Install", "Not Install")
group = c(1,2,2,3,4,4,4,5,6,6,6,8,8)
size = c(50,20,20,50,20,20,20,50,20,20,20,250,250)
hyperlink = c("http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://google.com", "http://yahoo.com", "http://yahoo.com")
MisNodes = data.frame(name, group, size, hyperlink)

ui = fluidPage(
  titlePanel("Testing app"),
    sidebarLayout(
    sidebarPanel(
      sliderInput("opacity", "Test", 0.6, min = 0.1, max = 1, step = .1)
    ),
    mainPanel(
      tabsetPanel(
        tabPanel("Force Network", forceNetworkOutput("force"))
      )
    )
  )
)

Myclickaction = "window.open(d.hyperlink, '_blank')"

server = function(input,output) {
  output$force = renderForceNetwork({
    fn <- forceNetwork(Links = MisLinks, Nodes = MisNodes,
      Source = "source", Target = "target", charge = -150,
      legend = TRUE, opacityNoHover = 1, Nodesize = "size",
      Value = "value", NodeID = "name",
      Group = "group", linkWidth = 2, clickAction =  Myclickaction,
      opacity = 0.9, colourScale =JS("d3.scaleOrdinal(d3.schemeCategory20);"),
      zoom=TRUE)
    fn$x$nodes$hyperlink <- hyperlink
    fn
  })
}
shinyApp(ui = ui, server = server)


另外,您将不得不在浏览器中打开闪亮的应用程序,因为在单独的RStudio查看器窗口中打开链接似乎无效。

关于javascript - 使用 Shiny 的clickAction将networkD3中的节点链接到网站,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/43065304/

10-12 17:55