8

我使用 R 中 networkD3 包创建的交互式输出。我知道如何将输出保存为 html 页面,但我还需要将图表的“静态”版本保存为 .png 文件。

代码如下所示:

# 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)

我可以通过单击“导出”然后“另存为图像”来保存输出。但是,我更喜欢在我的代码中使用一些命令来保存图片。

4

2 回答 2

13

只是对可能解决方案的更新。有一个名为webshot(由 W. Chang 等人编写)的包,它执行此渲染并截取 html 页面的屏幕截图。

例如用法:

webshot::webshot("file.html")

要获取 html 文件,您可能需要查看htmlwidgets::saveWidgetR. Vaidyanathan 等人的文章。


一个完全可重现的示例(保存simpleNetwork.png在您当前的工作目录中)

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")

在此处输入图像描述

于 2018-01-26T06:29:04.943 回答
3

我使用了此页面https://github.com/hafen/trelliscope/blob/master/R/thumb.R中的函数并试图简化它。

您需要从http://phantomjs.org/download.html安装 PhantomJS并在环境变量中设置路径。

该函数如下所示(参数p是 html 小部件,thumbName是新 .png 文件的名称):

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 文件保存到活动目录中:

# 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)
于 2016-10-27T14:59:42.073 回答