【问题标题】:Error in running Shiny app from specified dates. Warning: Error in : Result must have length 121728, not 0从指定日期运行 Shiny 应用程序时出错。警告:错误:结果的长度必须为 121728,而不是 0
【发布时间】:2020-01-15 21:48:51
【问题描述】:

我正在尝试创建一个 Shiny 应用程序,它采用数据集 (shiny_clean) 并创建一个 ggplot 线图,在 x 轴上显示模型功率预测与时间的关系。这是我的数据

Timestamp Turbine_ID Grd_Prod_Pwr_Avg Amb_WindSpeed_Max Amb_WindSpeed_Min Amb_WindSpeed_Avg Amb_WindSpeed_Std
1 2017-06-02 00:00:00        T07            891.6              14.2               2.8               7.8               1.4
2 2017-06-02 00:00:00        T06           1338.1              17.2               3.8               9.3               1.5
3 2017-06-02 00:00:00        T11            677.6              16.3               3.5               7.5               1.2
4 2017-06-02 00:00:00        T01            656.1              17.5               1.2               7.2               1.7
5 2017-06-02 00:10:00        T01            759.6              16.4               2.1               7.6               1.4
6 2017-06-02 00:10:00        T06           1145.0              13.9               3.3               8.6               1.3
  Amb_WindDir_Abs_Avg Nac_Direction_Avg Min_Windspeed1 Max_Windspeed1 Avg_Windspeed1 Var_Windspeed1 Min_Windspeed2
1               106.2              93.6            7.1           12.0            9.7           0.63            6.8
2                84.3              98.4            7.1           12.0            9.7           0.63            6.8
3                90.5              99.6            7.1           12.0            9.7           0.63            6.8
4               106.8             116.2            7.1           12.0            9.7           0.63            6.8
5                98.8             116.2            7.9           12.1           10.1           0.73            7.4
6                96.2              98.4            7.9           12.1           10.1           0.73            7.4
  Max_Windspeed2 Avg_Windspeed2 Var_Windspeed2 Min_AmbientTemp Max_AmbientTemp Avg_AmbientTemp Min_Pressure Max_Pressure
1           10.6            8.6           0.40              21              22              22         1008         1009
2           10.6            8.6           0.40              21              22              22         1008         1009
3           10.6            8.6           0.40              21              22              22         1008         1009
4           10.6            8.6           0.40              21              22              22         1008         1009
5           10.5            8.9           0.47              21              22              22         1008         1009
6           10.5            8.9           0.47              21              22              22         1008         1009
  Avg_Pressure Min_Humidity Max_Humidity Avg_Humidity   results     model
1         1009           68           69           68  788.8035 WGT_RF_NN
2         1009           68           69           68 1283.3277 WGT_RF_NN
3         1009           68           69           68  712.6971 WGT_RF_NN
4         1009           68           69           68  651.6901 WGT_RF_NN
5         1009           68           69           68  734.4558 WGT_RF_NN
6         1009           68           69           68  940.1862 WGT_RF_NN
structure(list(Timestamp = structure(c(1496361600, 1496361600, 
1496361600, 1496361600, 1496362200, 1496362200), class = c("POSIXct", 
"POSIXt"), tzone = "UTC"), Turbine_ID = structure(c(3L, 2L, 4L, 
1L, 1L, 2L), .Label = c("T01", "T06", "T07", "T11"), class = "factor"), 
    Grd_Prod_Pwr_Avg = c(891.6, 1338.1, 677.6, 656.1, 759.6, 
    1145), Amb_WindSpeed_Max = c(14.2, 17.2, 16.3, 17.5, 16.4, 
    13.9), Amb_WindSpeed_Min = c(2.8, 3.8, 3.5, 1.2, 2.1, 3.3
    ), Amb_WindSpeed_Avg = c(7.8, 9.3, 7.5, 7.2, 7.6, 8.6), Amb_WindSpeed_Std = c(1.4, 
    1.5, 1.2, 1.7, 1.4, 1.3), Amb_WindDir_Abs_Avg = c(106.2, 
    84.3, 90.5, 106.8, 98.8, 96.2), Nac_Direction_Avg = c(93.6, 
    98.4, 99.6, 116.2, 116.2, 98.4), Min_Windspeed1 = c(7.1, 
    7.1, 7.1, 7.1, 7.9, 7.9), Max_Windspeed1 = c(12, 12, 12, 
    12, 12.1, 12.1), Avg_Windspeed1 = c(9.7, 9.7, 9.7, 9.7, 10.1, 
    10.1), Var_Windspeed1 = c(0.63, 0.63, 0.63, 0.63, 0.73, 0.73
    ), Min_Windspeed2 = c(6.8, 6.8, 6.8, 6.8, 7.4, 7.4), Max_Windspeed2 = c(10.6, 
    10.6, 10.6, 10.6, 10.5, 10.5), Avg_Windspeed2 = c(8.6, 8.6, 
    8.6, 8.6, 8.9, 8.9), Var_Windspeed2 = c(0.4, 0.4, 0.4, 0.4, 
    0.47, 0.47), Min_AmbientTemp = c(21L, 21L, 21L, 21L, 21L, 
    21L), Max_AmbientTemp = c(22L, 22L, 22L, 22L, 22L, 22L), 
    Avg_AmbientTemp = c(22L, 22L, 22L, 22L, 22L, 22L), Min_Pressure = c(1008L, 
    1008L, 1008L, 1008L, 1008L, 1008L), Max_Pressure = c(1009L, 
    1009L, 1009L, 1009L, 1009L, 1009L), Avg_Pressure = c(1009L, 
    1009L, 1009L, 1009L, 1009L, 1009L), Min_Humidity = c(68L, 
    68L, 68L, 68L, 68L, 68L), Max_Humidity = c(69L, 69L, 69L, 
    69L, 69L, 69L), Avg_Humidity = c(68L, 68L, 68L, 68L, 68L, 
    68L), results = c(788.803513248849, 1283.32772621174, 712.697125017365, 
    651.690136285811, 734.455829490413, 940.186175814292), model = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "WGT_RF_NN", class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

我的用户界面工作正常。但是当我运行应用程序时,我收到警告错误:结果必须长度为 121728,而不是 0。我怀疑这与过滤有关,但我无法解决。这是我在运行应用程序时看到的。

这是我的代码:

library(shiny)
library(ggplot2)
library(tidyverse)
library(lubridate)

shiny_clean <- read.csv("shiny_clean.csv", stringsAsFactors = FALSE)
shiny_clean <- filter(shiny_clean, shiny_clean$Timestamp > as.Date("2017-06-01"))

ui <- fluidPage(
  titlePanel("Machine learning ensemble model to predict wind farm power output"),
    sidebarPanel(
      dateInput("dateInput", "Date", min = min(shiny_clean$Timestamp), max =max(shiny_clean$Timestamp), value = min(shiny_clean$Timestamp)),
      radioButtons("typeInput", "Turbine",
                   choices = c("T01", "T06", "T07", "T11"),
                   selected = "T01"),
      uiOutput("modelOutput")
    ),
  mainPanel(
    plotOutput("coolplot"),
    br(), br(),
    tableOutput("results")
  )
  )

server <- function(input, output) {

  output$modelOutput <- renderUI({
    selectInput("modelInput", "Model",
                sort(unique(shiny_clean$model)),
                selected = "WGT_RF_NN")
  })  

filtered <- reactive({
  if (is.null(input$modelInput)) {
    return(NULL)
  }    

  shiny_clean %>%
    filter(Timestamp == input$dateInput,
           Turbine_ID == input$typeInput,
           model == input$modelinput
    )
})

output$coolplot <- renderPlot({
  if (is.null(filtered())) {
    return()
  }
  ggplot()+
    geom_line(data = filtered, aes(x=Timestamp, y=results))
})

output$results <- renderTable({
  filtered()
})
}

shinyApp(ui = ui, server = server)

【问题讨论】:

    标签: r ggplot2 shiny


    【解决方案1】:

    在过滤部分进行了一些小的更正。您忘记了 input$modelInput 中的大写 i。时间戳需要调整为日期。在图中,您需要使用过滤器()而不是过滤器。

    这里是完整的代码:

    
    library(shiny)
    library(ggplot2)
    library(tidyverse)
    library(lubridate)
    
    shiny_clean <- read.csv("shiny_clean.csv", stringsAsFactors = FALSE)
    shiny_clean <- filter(shiny_clean, shiny_clean$Timestamp > as.Date("2017-06-01"))
    
    ui <- fluidPage(
      titlePanel("Machine learning ensemble model to predict wind farm power output"),
      sidebarPanel(
        dateInput("dateInput", "Date", min = min(shiny_clean$Timestamp), max =max(shiny_clean$Timestamp), value = min(shiny_clean$Timestamp)),
        radioButtons("typeInput", "Turbine",
                     choices = c("T01", "T06", "T07", "T11"),
                     selected = "T01"),
        uiOutput("modelOutput")
      ),
      mainPanel(
        plotOutput("coolplot"),
        br(), br(),
        tableOutput("results")
      )
    )
    
    server <- function(input, output) {
    
      output$modelOutput <- renderUI({
        selectInput("modelInput", "Model",
                    sort(unique(shiny_clean$model)),
                    selected = "WGT_RF_NN")
      })  
    
      filtered <- reactive({
        if (is.null(input$modelInput)) {
          return(NULL)
        }    
    
        shiny_clean %>%
          filter(as.Date(Timestamp) == as.Date(input$dateInput),
                 Turbine_ID == input$typeInput,
                 model == input$modelInput
          )
      })
    
      output$coolplot <- renderPlot({
        if (is.null(filtered())) {
          return()
        }
        ggplot()+
          geom_line(data = filtered(), aes(x=Timestamp, y=results))
      })
    
      output$results <- renderTable({
        filtered()
      })
    }
    
    shinyApp(ui = ui, server = server)
    
    

    【讨论】:

      猜你喜欢
      • 2019-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-14
      • 2021-11-09
      • 1970-01-01
      • 2020-02-23
      相关资源
      最近更新 更多