Search code examples
rplotvisnetwork

Visnetwork plot is not saved as png image


I plot a simple network using visNetwork but when I try to save it as png I get an empty image as a result.

png("ex.png")
require(visNetwork, quietly = TRUE)
# minimal example
nodes <- data.frame(id = 1:3)
edges <- data.frame(from = c(1,2), to = c(1,3))
visNetwork(nodes, edges, width = "100%")
dev.off()

Solution

  • You can save the network as html and then capture the content of that file:

    nodes <- data.frame(id = 1:3)
    edges <- data.frame(from = c(1,2), to = c(1,3))
    
    library(visNetwork)
    plot<- visNetwork(nodes, edges, width = "100%")
    
    html_name <- tempfile(fileext = ".html")
    visSave(plot, html_name)
    
    library(webshot); #webshot::install_phantomjs() #in case phantomjs was not installed 
    
    webshot(html_name, zoom = 2, file = "ex.png")