Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

two dygraph in RStudio Viewer pane

Is it possible to simultaneously view two dygraphs in RStudio Viewer pane?

I am updating an old function that produces two time-series plots (using ggplot2), stacked one above the other (using gridExtra::grid.arrange(ggp1, ggp2)). I want to use the cool dygraph and the change is pretty straightforward, ..., except that I would like to view both plots simultaneously in RStudio Viewer pane.

It is possible to view one plot at a time. Indeed, "If you call dygraph within RStudio then it’s output appears within the Viewer pane". But I couldn't find a trick to display both plots at the same time. And I want that, because I want to use the handy synchronization feature of dygraph

For the sake of a reproducible example, here's an example of what I am doing.

library(dygraphs)   
dygraph(mdeaths, group = "ensync")
dygraph(fdeaths, group = "ensync")

But every one of these is a new call in R Console and then the first plot is displayed on the viewer and immediately the second plot replaces it.

The only workaround I found is put it in an RMarkdown document and knitr it all. But I kind of dislike this approach. It would be more convenient for me directly displaying it in RStudio Viewer pane.

Any ideas?

like image 995
elikesprogramming Avatar asked May 26 '16 21:05

elikesprogramming


1 Answers

Using htmltools package, insert the two dygraphs in a tagList and use the browsable() function to plot both graphs in the viewer:

library(dygraphs)
library(htmltools)

browsable(
  tagList(
    dygraph(mdeaths, group = "ensync", height = 200, width = "100%"),
    dygraph(fdeaths, group = "ensync", height = 200, width = "100%")
    )
  )

Maybe the code is more readable with the magrittr forward-pipe operator :

library(dygraphs)
library(htmltools)
library(magrittr)

dygraph(mdeaths, group = "ensync", height = 200, width = "100%") %>%
  tagList(dygraph(fdeaths, group = "ensync", height = 200, width = "100%")) %>%
  browsable()
like image 89
RLesur Avatar answered Nov 04 '22 20:11

RLesur