[英]For loop over dygraph does not work in R
dygraph
上有一些奇怪的行為。
對dygraph
使用for
循環時,我沒有得到任何結果。
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
for(i in 1:2){
dygraph(lungDeaths[, i])
}
另一方面,當我使用lapply
我確實得到了預期的結果
lapply(1:2, function(i) dygraph(lungDeaths[, i]))
我實際上想在我自己的數據集上使用R Markdown
的for
循環並迭代不同的列,但即使我使用lapply
“解決方法”,它也不會繪制dygraphs
R 降價代碼
---
title: "Untitled"
author: "dimitris_ps"
date: "28 May 2015"
output: html_document
---
```{r}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
lapply(1:2, function(i) dygraph(lungDeaths[, i]))
```
而當我逐列運行它時,它會起作用
---
title: "Untitled"
author: "dimitris_ps"
date: "28 May 2015"
output: html_document
---
```{r echo=FALSE}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
```
```{r}
dygraph(lungDeaths[, 1])
dygraph(lungDeaths[, 2])
```
任何幫助將不勝感激。
會話信息
R version 3.2.0 (2015-04-16)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252 LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] dygraphs_0.4.3 devtools_1.7.0
loaded via a namespace (and not attached):
[1] htmltools_0.2.6 tools_3.2.0 yaml_2.1.13 rmarkdown_0.6.1 digest_0.6.8
只需將lapply()
的列表輸出包裝在htmltools::tagList()
,例如
```{r}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
res <- lapply(1:2, function(i) dygraph(lungDeaths[, i]))
htmltools::tagList(res)
```
對於任何在循環中掙扎的人來說,這對我有用。
p=list()
for (n in 1:3){
p[[n]] <- plot_ly(x = 1:100, y = rnorm(100)+n, mode = 'lines', type="scatter")
}
htmltools::tagList(p)
即,列表 p 是在循環中創建還是在 lapply 等中創建並不重要,只要您在循環外調用 htmltools::tagList 即可。
感謝 Yihui 幫助我到達那里,並在開發和幫助這些工具方面付出了巨大的努力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.