Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple d3heatmaps not displaying in conditional #54

Closed
bextra opened this issue Mar 11, 2016 · 6 comments
Closed

Multiple d3heatmaps not displaying in conditional #54

bextra opened this issue Mar 11, 2016 · 6 comments

Comments

@bextra
Copy link

bextra commented Mar 11, 2016

When trying to plot multiple d3heatmaps within a conditional, the HTML that is generated only plots the final heat map. Below is code for an Rmd to reproduce the bug.


---
title: "test"
output: html_document

---

This is an R Markdown document. 

```{r}
require(d3heatmap)
myVar = TRUE
if (myVar == TRUE) {
  d3heatmap(mtcars, col = "Spectral")
  d3heatmap(mtcars, col = "Blues")
}
```

If the heat maps are outside of a conditional, multiple will appear in the knitted html file. Has anyone else run into this? Any help would be appreciated.

sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.3 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] d3heatmap_0.6.1.1

loaded via a namespace (and not attached):
 [1] htmlwidgets_0.6 magrittr_1.5    htmltools_0.3   tools_3.2.3     base64enc_0.1-3
 [6] yaml_2.1.13     stringi_1.0-1   rmarkdown_0.9.5 knitr_1.12.3    stringr_1.0.0  
[11] digest_0.6.9    evaluate_0.8.3  png_0.1-7  
@timelyportfolio
Copy link

I think using tagList() from htmltools will solve your problem.

Try

library(htmltools)
require(d3heatmap)
myVar = TRUE
if (myVar == TRUE) {
  tagList(list(
    d3heatmap(mtcars, col = "Spectral"),
    d3heatmap(mtcars, col = "Blues")
  ))
}

@bextra
Copy link
Author

bextra commented Mar 11, 2016

Thank you especially for the quick response. This is a nice work around. Am I missing something or should I be able to generate multiple d3heatmap just like any plot within a code chunk though?

@timelyportfolio
Copy link

I can't say for sure, but I'm almost positive we are limited to one htmlwidget per code chunk. This might help explain ramnathv/htmlwidgets#110.

@bextra
Copy link
Author

bextra commented Mar 11, 2016

That's what I initially thought too. But this works just fine.

require(d3heatmap)
d3heatmap(mtcars, col = "Spectral")
d3heatmap(mtcars, col = "Blues")

@bextra bextra closed this as completed Mar 12, 2016
@jcheng5
Copy link
Collaborator

jcheng5 commented Mar 12, 2016

Calling d3heatmap doesn't actually render a heatmap, it just creates and returns one--just like all htmlwidgets. (This is different than plots which actually get created when you call plot()). It just so happens that what knitr chooses to do with top-level expressions, is print (actually knit_print) them.

So what you did was akin to this:

if (myVar == TRUE) {
  1
  2
}

And wondering why only 2, not 1, was printed. And tagList is akin to wrapping these numbers in c(1, 2).

So the limitation if you want to call it that, is that an if() block only ever returns its last expression.

@bextra
Copy link
Author

bextra commented Mar 12, 2016

That makes perfect sense. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants