save heatmap generated by d3heatmap
Asked Answered
K

0

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")
```
Katabolism answered 16/3, 2016 at 16:32 Comment(10)
Install the dev version of knitr. It has functionality from the new webshot package to facilitate this.Billingsley
thanks for the tip, @hrbrmstr. investigating.Katabolism
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?Katabolism
Um… github.com/yihui/knitr/blob/master/NEWS.md ? (it's clearly in knitr dev.Billingsley
Separate example of just using webshot: github.com/hrbrmstr/vegalite/blob/master/R/phantom.rBillingsley
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.Katabolism
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.Katabolism
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. https://mcmap.net/q/1550899/-using-webshot-within-knitrKatabolism
Related: https://mcmap.net/q/1633950/-render-leaflet-plots-as-raster-in-rSchaffel
@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.Reeta

© 2022 - 2024 — McMap. All rights reserved.