如何在R中将HTML输出捕获为PNG [英] How to capture html output as png in R
问题描述
我使用R中networkD3包创建的交互式输出.我知道如何将输出保存为html页面,但是我还需要将图的静态"版本另存为.png文件.
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.
代码如下:
# 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.
推荐答案
我使用了此页面中的函数 https://github.com/hafen/trelliscope/blob/master/R/thumb.R 并试图简化它.
I used function from this page https://github.com/hafen/trelliscope/blob/master/R/thumb.R and tried to simplify it.
您需要从 http://phantomjs.org/download.html 安装PhantomJS,环境变量中设置的路径.
You need to have PhantomJS installed from http://phantomjs.org/download.html and the path set in environment variables.
函数如下所示(参数p
是html小部件,thumbName
是新.png文件的名称):
The function looks like this (param p
is the html widget, thumbName
is the name of new .png file):
library(htmlwidgets)
widgetThumbnail <- function(p, thumbName, width = 1024, height = 768) {
phantom <- findPhantom()
success <- FALSE
if(phantom == "") {
message("** phantomjs dependency could not be found - thumbnail cannot be generated (run phantomInstall() for details)")
} else {
res <- try({
ff <- paste0(thumbName, ".html")
ffjs <- paste0(thumbName, ".js")
# don't want any padding
p$sizingPolicy$padding <- 0
suppressMessages(saveWidget(p, ff, selfcontained = FALSE))
js <- paste0("var page = require('webpage').create();
page.viewportSize = { width: ", width,", height: ", height," };
page.clipRect = { top: 0, left: 0, width: ", width,", height: ", height," };
page.open('", ff, "', function(status) {
console.log(\"Status: \" + status);
if(status === \"success\") {
page.render('", thumbName, ".png');
}
phantom.exit();
});")
cat(js, file = ffjs)
system2(phantom, ffjs)
})
if(!inherits(res, "try-error")) {
success <- TRUE
}
if(!file.exists(paste0(thumbName, ".png"))) {
success <- FALSE
}
}
if(!success) {
message("** could not create htmlwidget thumbnail... creating an empty thumbnail...")
}
}
#' Get instructions on how to install phantomjs
#' @export
phantomInstall <- function() {
message("Please visit this page to install phantomjs on your system: http://phantomjs.org/download.html")
}
# similar to webshot
findPhantom <- function() {
phantom <- Sys.which("phantomjs")
if(Sys.which("phantomjs") == "") {
if(identical(.Platform$OS.type, "windows")) {
phantom <- Sys.which(file.path(Sys.getenv("APPDATA"), "npm", "phantomjs.cmd"))
}
}
phantom
}
它将创建.js文件,该文件将使用您的html小部件,捕获屏幕并将.js,.html和.png文件保存到活动目录中:
It creates .js file, which takes your html widget, captures the screen and saves .js, .html and .png files into active directory:
# 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
plot = simpleNetwork(networkData)
# Save html as png
widgetThumbnail(p = plot, thumbName = "plot", height = 500)
这篇关于如何在R中将HTML输出捕获为PNG的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!