For dygraph的循环在R中不起作用

问题描述 投票:11回答:1

dygraph上有一些奇怪的行为。

for循环使用dygraph时,我没有结果。

library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)

for(i in 1:2){
  dygraph(lungDeaths[, i])
}

另一方面,当我使用lapply时,我确实得到了预期的结果

lapply(1:2, function(i) dygraph(lungDeaths[, i]))

我实际上想在我自己的数据集上使用for中的R Markdown循环并遍历不同的列,但是即使我使用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 

在笔形图上有一些奇怪的行为。当使用for dygraph循环时,我没有任何结果。文库(笔画)肺部死亡

r r-markdown dygraphs
1个回答
14
投票

只需将lapply()中的列表输出包装到htmltools::tagList()中,例如

© www.soinside.com 2019 - 2024. All rights reserved.