Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture html output as png in R

I use interactive output created by networkD3 package in R. I know how to save the output as html page, but I also need to save the 'static' version of the diagram as .png file.

The code looks like this:

# Load package
library(networkD3)

# Create fake data
src <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)

# Plot
simpleNetwork(networkData)

I can save the output by clicking on 'Export' and then 'Save as Image'. However, I prefer to use some commands in my code to save the picture.

like image 899
Lenka Vraná Avatar asked Jan 28 '16 08:01

Lenka Vraná


1 Answers

just an update to the possible solutions. There is a package called webshot (by W. Chang, et al.) that does this rendering and taking screenshots of html pages.

e.g usage:

webshot::webshot("file.html")

And to get the html file, you might want to check out htmlwidgets::saveWidget by R. Vaidyanathan, et al.


a fully reproducible example (saves simpleNetwork.png in your current working directory)

library(networkD3)

src <- c("A", "A", "A", "A", "B", "B", "C", "C", "D")
target <- c("B", "C", "D", "J", "E", "F", "G", "H", "I")
networkData <- data.frame(src, target)

sn <- simpleNetwork(networkData)
saveNetwork(sn, "sn.html")

library(webshot)
webshot("sn.html", "simpleNetwork.png")

enter image description here

like image 70
chinsoon12 Avatar answered Sep 28 '22 12:09

chinsoon12