Search code examples
rshinydygraphsr-dygraphs

dygraph inside an absolute panel


I have a leaflet map in my main panel and I want to place a dygraph inside an absolutePanel within an R shiny app. My problem is that I can't see the dygraph inside the absolutePanel.

The code in my ui.R is like this:

library(dygraphs)

absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
          draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
          width = 300, height = "auto",

          h2("Sensitivity Explorer"),
          sliderInput(inputId="year",
                      label="Select a forecast year",
                      value=2018, min=2018, max=2050),



          numericInput("months", label = "Months to Predict", 
                       value = 72, min = 12, max = 144, step = 12),
          selectInput("interval", label = "Prediction Interval",
                      choices = c("0.80", "0.90", "0.95", "0.99"),
                      selected = "0.95"),
          checkboxInput("showgrid", label = "Show Grid", value = TRUE),

          dygraphOutput("dygraph",width = '50%')

                    )

and my server.R :

library(dygraphs)
function(input, output, session) {

predicted <- reactive({
hw <- HoltWinters(ldeaths)
predict(hw, n.ahead = input$months, 
        prediction.interval = TRUE,
        level = as.numeric(input$interval))
})
output$dyngraph <- renderDygraph({
if (nrow(zipsInBounds()) == 0)
  return(NULL)
dygraph(predicted(), main = "Predicted Deaths/Month") %>%
  dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>%
  dyOptions(drawGrid = input$showgrid)

 })
 }

Solution

  • Make sure you test for null first, also make use of req to find out how it works just type?req. Also its dyngraph btw

    rm(list = ls())
    library(shiny)
    library(dygraphs)
    ui <- fluidPage(
            absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE,
                          draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto",
                          width = 300, height = "auto",
    
                          h2("Sensitivity Explorer"),
                          sliderInput(inputId="year",
                                      label="Select a forecast year",
                                      value=2018, min=2018, max=2050),
    
    
    
                          numericInput("months", label = "Months to Predict", 
                                       value = 72, min = 12, max = 144, step = 12),
                          selectInput("interval", label = "Prediction Interval",
                                      choices = c("0.80", "0.90", "0.95", "0.99"),
                                      selected = "0.95"),
                          checkboxInput("showgrid", label = "Show Grid", value = TRUE),
    
                          dygraphOutput("dyngraph",width = '50%')
    
            )
    
    )
    
    
    
    server <- function(input, output, session){
    
            zipsInBounds <- reactive({mtcars[0,0]})
    
            predicted <- reactive({
                    req(input$interval)
                    req(input$months)
                    hw <- HoltWinters(ldeaths)
                    predict(hw, n.ahead = as.numeric(input$months), 
                            prediction.interval = TRUE,
                            level = as.numeric(input$interval))
            })
            output$dyngraph <- renderDygraph({
                    if (is.null(zipsInBounds()))
                            return()
                    dygraph(predicted(), main = "Predicted Deaths/Month") %>%
                            dySeries(c("lwr", "fit", "upr"), label = "Deaths") %>%
                            dyOptions(drawGrid = input$showgrid)
    
            })
    
    
    }
    shinyApp(ui = ui, server=server)