【问题标题】:create empty dygraph in Rshiny在 R Shiny 中创建空的 dygraph
【发布时间】:2017-12-26 16:04:08
【问题描述】:

我正在使用 Dygraphs 包来可视化 R Shiny 中的实际值和时间序列预测值。这是我用来生成 Dygraph 的示例代码。在某些数据点较少的情况下,Holt Winters(gamma =T) 没有给出任何预测,我需要显示一个标题为“Insufficient Data”的空 Dygraph)。我无法做到这一点。感谢您对此的任何帮助

library(dygraphs)

plotDyg <- fluidPage(
  fluidRow(
    box(selectizeInput("c1", "Enter a key",
                         choices = reactive({sort(unique(df$key))})(),
                         multiple = FALSE),width=3),
    box(dygraphOutput("tsDy"), width = 10, height = 500))
)

ui <- dashboardPage(
  dashboardHeader(title = "XYZ"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("abc", tabName = "sidebar2", icon = icon("bar-chart") ,
               menuSubItem("def",icon = icon("folder-open"), tabName = "subMenu1")
               )
      )
  ),
  dashboardBody(
    tabItems(
      tabItem(tabName = "subMenu1", 
              fluidRow(
                tabBox(
                  title = "ghi", id = "tabset2",height = "1500px",width = 100,
                  tabPanel("abcdef", plotDyg)
                )
              )
      )

    )

  )
)

 server <- function(input, output) {
  output$tsDy <- renderDygraph({
    if(!is.null(input$c1)){
      df.0 <- reactive({df[df$key == input$c1,]})()
      tspred <- reactive({
                df.0 <- convert_to_ts(df.0)  # converts column "fin_var" to a monthly time series and returns the entire dataframe
                act <- df.0$fin_var
                hw <- tryCatch(HoltWinters(df.0$fin_var), error=function(e)NA)
                if(length(hw) > 1){
                  p <- predict(hw, n.ahead = 12, prediction.interval = TRUE, level = 0.95)
                  all1 <- cbind(act, p)
                }else{all1 <- matrix()}
         })
         if(!is.na(tspred())){
           dygraph(tspred(), main = "TS Predictions") %>%
           dySeries("act", label = "Actual") %>%
           dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Predicted") %>%
           dyOptions(drawGrid = F) %>%
           dyRangeSelector()
         }else{dygraph(matrix(0), main = "Insufficient Data")} # I could just do 'return()' but I want to show an empty Dygraph with the title
    }else{return()}
  })
}

【问题讨论】:

  • 为什么你的 plotDyg 在 ui 之外?
  • 只是为了更好的可读性。我在tabpanel部分调用plotDyg函数tabPanel("abcdef", plotDyg)

标签: r shiny dygraphs shinyjs


【解决方案1】:

我也无法使用空时间序列渲染 Dygraph。为了向用户呈现消息,我使用了validate/need functions in Shiny

在你的情况下,我会替换

 if(!is.na(tspred())){

 validate(need(!is.na(tspred())), "Insufficient Data"))

这将避免 Dygraphs 中的“错误:参数长度为零”消息,并向最终用户打印适当的消息。

【讨论】:

    猜你喜欢
    • 2018-08-19
    • 2017-03-12
    • 2016-12-03
    • 2017-06-22
    • 1970-01-01
    • 2018-11-01
    • 2021-12-02
    • 2017-01-30
    • 2016-10-26
    相关资源
    最近更新 更多