[R] dygraphs, multiple graphs and shiny

Roy Mendelssohn - NOAA Federal roy.mendelssohn at noaa.gov
Wed Oct 18 19:43:06 CEST 2017


Hi All:

This is really getting into the weeds,  but I am hoping someone will have a solution.  I am trying to use dygrahs for R, within Shiny.

The situation arises when I am combining a number of dygraphs into one plot.  If I am just in an RNotebook, if you look at:

https://stackoverflow.com/questions/30509866/for-loop-over-dygraph-does-not-work-in-r

the solution to have the plot shown from a RNotebook is code like this:

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

and if you put that into an RNotebook and knit it, it works.  Okay in order to have a reproducible example,  I now try to create a Shiny App using that example,  and the template generated by RStudio.  To use dygraphs in Shiny,  you replace the normal render and plot routines,  such that the following "works"  in the sense when run the graph and slider are shown - this is showing just a single digraph:

> library(shiny)
> 
> # Define UI for application that draws a histogram
> ui <- fluidPage(
>    
>    # Application title
>    titlePanel("Test"),
>    
>    # Sidebar with a slider input for number of bins 
>    sidebarLayout(
>       sidebarPanel(
>          sliderInput("bins",
>                      "Number of bins:",
>                      min = 1,
>                      max = 50,
>                      value = 30)
>       ),
>       
>       # Show a plot of the generated distribution
>       mainPanel(
>         dygraphs::dygraphOutput("distPlot")
>       )
>    )
> )
> 
> # Define server logic required to draw a histogram
> server <- function(input, output) {
>    
>    output$distPlot <- dygraphs::renderDygraph({
>      lungDeaths <- cbind(mdeaths, fdeaths)
>      res <- lapply(1:2, function(i) dygraph(lungDeaths[, i]))
>      dygraph(lungDeaths[, 1])
>    })
> }
> 
> # Run the application 
> shinyApp(ui = ui, server = server)
> 


Now make the single change to try and render the combined plot:

> library(shiny)
> 
> # Define UI for application that draws a histogram
> ui <- fluidPage(
>    
>    # Application title
>    titlePanel("Test"),
>    
>    # Sidebar with a slider input for number of bins 
>    sidebarLayout(
>       sidebarPanel(
>          sliderInput("bins",
>                      "Number of bins:",
>                      min = 1,
>                      max = 50,
>                      value = 30)
>       ),
>       
>       # Show a plot of the generated distribution
>       mainPanel(
>         dygraphs::dygraphOutput("distPlot")
>       )
>    )
> )
> 
> # Define server logic required to draw a histogram
> server <- function(input, output) {
>    
>    output$distPlot <- dygraphs::renderDygraph({
>      lungDeaths <- cbind(mdeaths, fdeaths)
>      res <- lapply(1:2, function(i) dygraph(lungDeaths[, i]))
>      htmltools::tagList(res)
>    })
> }
> 
> # Run the application 
> shinyApp(ui = ui, server = server)
> 



If you run the second example,  the plot does not appear.

Thanks for any help.

-Roy

**********************
"The contents of this message do not reflect any position of the U.S. Government or NOAA."
**********************
Roy Mendelssohn
Supervisory Operations Research Analyst
NOAA/NMFS
Environmental Research Division
Southwest Fisheries Science Center
***Note new street address***
110 McAllister Way
Santa Cruz, CA 95060
Phone: (831)-420-3666
Fax: (831) 420-3980
e-mail: Roy.Mendelssohn at noaa.gov www: http://www.pfeg.noaa.gov/

"Old age and treachery will overcome youth and skill."
"From those who have been given much, much will be expected" 
"the arc of the moral universe is long, but it bends toward justice" -MLK Jr.



More information about the R-help mailing list