将 networkD3 中节点上的 clickAction 链接到本地​​ HTML 文件

linking clickAction on a node in networkD3 to a local HTML file

您好,我正在尝试 R 中 networkD3 包中的 forcenetwork 函数。

我被 clickAction 卡住了。 clickAction 的目的是打开一个新的 window 以显示本地 HTML 文件的内容。但是现在,添加一个网站URL就可以了,比如http://www.google.com。但是我无法打开本地 HTML 文件。

当我添加本地 HTML 文件的方向时,它不起作用并显示 "Not Found"。

我试图将所有 HTML 文件放在相对路径的 www 文件中。但是没用。

我想知道是否有任何方法可以在 clickAction 中显示本地 HTML 文件? 谢谢!

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("file:///C:/user/docname.html", "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)

使用 addResourcePath 函数在 运行 shinyApp 之前分配一个闪亮的可访问目录,例如addResourcePath('sub_dir', 'www')

然后确保您的本地文件的 hyperlinks 适合于此,例如hyperlink = c("sub_dir/docname.html", "http://yahoo.com")

并确保你想要 link 的 file/s 位于你用 addResourcePath 的第二个参数指定的目录中(你可以指定相对于当前目录的目录R 中的工作目录,或具有有效的绝对路径)

所以它可能看起来像...

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("sub_dir/docname.html", "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
  })
}
addResourcePath('sub_dir', 'www')
shinyApp(ui = ui, server = server)