2015-05-28 30 views
8

dygraph上有一些奇怪的行爲。For循環結束dygraph在R中不起作用

當使用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])) 

其實我是想用我自己的數據集在R Markdownfor環和遍歷不同的列,但即使當我使用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 
+0

'lapply'在'markdown'中爲我工作。至於'for循環',你可以通過使用'show(dygraph(lungDeaths [,i]))' – user227710

+0

感謝您研究這個問題來解決問題(低效的方式)。不幸的是我對這兩件事都沒有工作我會更新我的問題,我的'sessionInfo()' –

+0

我使用相同的R版本加'0.4.3'dygraphs版本。 – user227710

回答

6

只是包裝從lapply()輸出列表中htmltools::tagList(),例如

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

謝謝,你的上師。 –