3

I'm trying to save a heatmap produced by d3heatmap to a pdf file, but the file is always damaged.

library(d3heatmap)
pdf(file="heat.pdf")
d3heatmap(mtcars, scale = "column", colors = "Spectral")
dev.off()

I suspect the challenge is "this is an R package that implements a heatmap htmlwidget." However, I figured I could still capture the image produced because "Like any htmlwidget, you can visualize a d3 heatmap directly from the R console."

I looked at the two output functions in the package, renderD3heatmap and d3heatmapOutput, but I don't think either one gets me where I want to go.

Update I followed @hrbrmstr's advice to look into knitr and webshot. See this answer about how to get them to play nice. Here's my latest attempt to produce an image file. I think I'm on the right track with screenshot.opts, but I'm getting a file about a bad connection.

---
title: "Untitled"
output: pdf_document
---
```{r, setup}
library(d3heatmap)
library(knitr) # dev't version knitr_1.12.22 (also dev't version of webshot 0.3)
```

```{r, screenshot.opts=list(file="webshot.png", cliprect="viewport", selector = NULL)}
d3heatmap(mtcars, scale = "column", colors = "Spectral")
```
Community
  • 1
  • 1
Eric Green
  • 7,385
  • 11
  • 56
  • 102
  • 1
    Install the dev version of `knitr`. It has functionality from the new `webshot` package to facilitate this. – hrbrmstr Mar 16 '16 at 16:34
  • thanks for the tip, @hrbrmstr. investigating. – Eric Green Mar 16 '16 at 16:41
  • I don't see the `webshot` function in the development version of `knitr`, so I downloaded the `webshot` package directly. Seems like it wants me to feed it a URL. Any pointers to examples of folks using this package (or from within knitr if I'm doing something wrong) to capture this widget? – Eric Green Mar 16 '16 at 16:49
  • Um… https://github.com/yihui/knitr/blob/master/NEWS.md ? (it's clearly in knitr dev. – hrbrmstr Mar 16 '16 at 16:51
  • Separate example of just using webshot: https://github.com/hrbrmstr/vegalite/blob/master/R/phantom.r – hrbrmstr Mar 16 '16 at 16:52
  • thanks for the links. i was expecting to find a `webshot` function in the development version of `knitr`, but maybe I shouldn't have. going to play around with this. – Eric Green Mar 16 '16 at 16:56
  • i think i'm close to getting it to work in knitr, but i have a problem after following the instruction to install `webshot::install_phantomjs()`. i get the message: `phantomjs.exe has been installed to /Users/username/Library/Application Support/PhantomJS` however when i run, i get the error that `sh: ~/Library/Application Support/PhantomJS/phantomjs: No such file or directory`. i think that i need to update the path, but i'm not sure how. – Eric Green Mar 16 '16 at 17:11
  • I created a separate question about this problem. Will come back here once working to close the loop on how to capture the heatmap image. http://stackoverflow.com/questions/36043244/using-webshot-within-knitr – Eric Green Mar 16 '16 at 17:51
  • Related: http://stackoverflow.com/a/35550394/2706569 – CL. Mar 16 '16 at 18:28
  • @EricGreen Did you eventually get this to work for you (i.e., successfully produce a PDF from the d3heatmap)? It seems to be an unsolved problem. – warship May 17 '16 at 19:28

0 Answers0