12

There is some strange behavior on dygraph.

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

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

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

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 code

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

Any help will be greatly appreciated.

Session info

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 
dimitris_ps
  • 5,849
  • 3
  • 29
  • 55
  • `lapply` works in `markdown` for me. As to `for loop`, you can fix the problem (inefficient way) by using `show(dygraph(lungDeaths[, i]))` – user227710 May 28 '15 at 15:48
  • Thanks for looking into this. Unfortunately for me neither work. I will update my question with my `sessionInfo()` – dimitris_ps May 28 '15 at 16:28
  • I am using the same R version plus ‘0.4.3’ dygraphs version. – user227710 May 28 '15 at 16:38
  • 2
    Same here. This is strange. I have tried it on 2 different machines but is still have the issue stated. – dimitris_ps May 28 '15 at 16:48
  • When I say `lapply` and `show` option in `for loop` work, I mean I can only see in `viewer `not in `html`. May be this has to do with `dynamic chart`. You can shoot package maintainer an email on this. – user227710 May 28 '15 at 17:21

2 Answers2

17

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)
```
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Thanks, your the guru. – dimitris_ps Feb 07 '16 at 23:04
  • For shiny, use renderUI( htmltools::tagList(res) ) in the server and then htmlOutput() in the UI. (Rather than renderDygraph() and dygraphOutput()). Here's an example that helped me in Shiny: https://www.mail-archive.com/r-help@r-project.org/msg246267.html – Hayward Oblad Dec 14 '21 at 18:37
1

For anyone struggling with a loop, here's what worked for me.

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)

I.e. it doesn't matter if the list p is created in a loop or lapply, etc. as long as you call htmltools::tagList outside the loop.

Thanks to Yihui for helping me get there and for immense work developing and helping with these tools.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • If you want to add some commentary about this answer - such as whether it is a duplicate answer - you could put it in the comments here under your answer. – halfer Feb 09 '20 at 22:47
  • Additional hint: instead of *renderDygraph* one has to use *renderUI*. – lambruscoAcido Feb 18 '20 at 09:19