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

徘徊边缘 提交于 2019-12-08 08:59:43

问题


Hi I am trying forcenetwork function in networkD3 package in R.

I get stuck with clickAction. The aim of clickAction is to open a new window to show the content of a local HTML file. But now, It is ok to add a website URL like http://www.google.com. But I get stuck with opening local HTML file.

When I add the direction of local HTML file, it doesn't work and show "Not Found".

I have tried to put all HTML files in www file in relative path. But it doesn't work.

I was wondering if there is any way to show the local HTML file in clickAction? Thanks!

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)

回答1:


Use the addResourcePath function to assign a shiny accessible directory before running shinyApp, e.g. addResourcePath('sub_dir', 'www')

then make sure your hyperlinks to local files are appropriate to that, e.g. hyperlink = c("sub_dir/docname.html", "http://yahoo.com")

and make sure the file/s you want to link to are in the directory you specified with the 2nd argument of addResourcePath (you can specify the directory either relative to your current working directory in R, or with a valid absolute path)

so it might look like...

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)


来源:https://stackoverflow.com/questions/49538023/linking-clickaction-on-a-node-in-networkd3-to-a-local-html-file

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!