For dygraph 循环在 R 中不起作用 [英] For loop over dygraph does not work in R

查看:51
本文介绍了For dygraph 循环在 R 中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

dygraph 上有一些奇怪的行为.

There is some strange behavior on dygraph.

当对 dygraph 使用 for 循环时,我没有得到任何结果.

When using a for loop for dygraph i get no result.

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

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

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

On the other hand when i use lapply i do get the expected result

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

我实际上想在我自己的数据集上使用 R Markdown 中的 for 循环并迭代不同的列,但即使我使用 lapply 解决方法",它不会绘制 dygraphs

I actually want to use the for loop in R Markdown on my own data set and iterate over the different columns, but even when i use the lapply "workaround", it does not plot the dygraphs

R Markdown 代码

---
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]))
```

而当我逐列运行它时它会起作用

Whereas when i run it column by column it works

---
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() 中,例如

Just wrap the list output from lapply() in htmltools::tagList(), e.g.

```{r}
library(dygraphs)
lungDeaths <- cbind(mdeaths, fdeaths)
res <- lapply(1:2, function(i) dygraph(lungDeaths[, i]))
htmltools::tagList(res)
```

这篇关于For dygraph 循环在 R 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆