[英]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.