绝对面板内的dygraph

时间:2018-03-03 05:57:52

标签: r shiny dygraphs r-dygraphs

我的主面板中有一张传单地图,我想在一个R闪亮的应用程序中的一个绝对面板内放置一个dygraph。 我的问题是我无法在absolutePanel中看到dygraph。

我的ui.R中的代码是这样的:

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%')

                    )

和我的服务器.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)

 })
 }

1 个答案:

答案 0 :(得分:0)

请务必先测试null,并使用req查看其工作原理,然后输入?req。还有它的dyngraph顺便说一句

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)