dygraphs, multiple graphs and shiny

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

dygraphs, multiple graphs and shiny

Roy Mendelssohn - NOAA Federal
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: [hidden email] 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: dygraphs, multiple graphs and shiny

Roy Mendelssohn - NOAA Federal
Answering my own question.  It took a lot of trial and error,  but the code below will work.  The trick is to do form the lis to plots, create the html tag, and use renderUI() for that, and then in the UI.R part use  htmlOutput() to output the result.

-Roy


> 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")
>         htmlOutput("distPlot")
>       )
>    )
> )
>
> # Define server logic required to draw a histogram
> server <- function(input, output) {
>   lungDeaths <- cbind(mdeaths, fdeaths)
>  # output$distPlot <- dygraphs::renderDygraph({
>         res = list()
>         res[[1]] <-  dygraph(lungDeaths[, 1], group = 'lungs') %>% dyRangeSelector()
>         res[[2]] <-  dygraph(lungDeaths[, 1], group = 'lungs') %>% dyRangeSelector()
>        res <- htmltools::tagList(res)
>        output$distPlot <- renderUI({
>         res
>        })
> }
>
> # Run the application
> shinyApp(ui = ui, server = server)
>




> On Oct 18, 2017, at 10:43 AM, Roy Mendelssohn - NOAA Federal <[hidden email]> wrote:
>
> 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: [hidden email] 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.
>

**********************
"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: [hidden email] 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.

______________________________________________
[hidden email] mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.