[英]Issue plotting multiple d3heatmaps in conditional with knitr
当尝试在条件中绘制多个 d3heatmaps 时,生成的 HTML 仅绘制最终的热图。 下面是 RMD 重现错误的代码。
---
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")
}
```
如果热图在条件之外,多个将出现在编织的 HTML 文件中。 有没有其他人遇到过这个问题?
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
Yihui在这个答案中解释了潜在的问题:HTML 小部件“仅在从顶级R 表达式生成时才有效”。 这解释了为什么将图包装在if
语句中不起作用。 尽管如此,第二个图仍然可见,因为if
返回最后一个表达式的值,从而将最后一个图“提升”到顶层。
作为一种解决方案,可以将这些图收集在一个列表中,并按照此处的建议通过htmltools::tagList
打印:
```{r}
require(d3heatmap)
myVar = TRUE
res <- list()
if (myVar == TRUE) {
res[[1]] <- d3heatmap(mtcars, col = "Spectral")
res[[2]] <- d3heatmap(mtcars, col = "Blues")
}
htmltools::tagList(res)
```
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.