Roy Mendelssohn - NOAA Federal
2017-Oct-18 17:43 UTC
[R] dygraphs, multiple graphs and shiny
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.
Roy Mendelssohn - NOAA Federal
2017-Oct-18 21:10 UTC
[R] dygraphs, multiple graphs and shiny
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 <roy.mendelssohn at noaa.gov> 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: 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. >********************** "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.
Possibly Parallel Threads
- [VC++ calling R] How to create a real-time interactive ticking time-series chart using dygraph via RInside?
- [VC++ calling R] How to create a real-time interactive ticking time-series chart using dygraph via RInside?
- need held in r coding.
- problema con shiny
- Discrepancia entre Ord_plot y distplot del paquete vcd