0

我一直在开发一个使用auto.arima()包中的闪亮应用程序以及一个xreg参数。由于某种原因,该xreg函数(采用数据帧或矩阵)已停止接受反应函数的输出 - 这是一个子集数据帧。解决方法很糟糕,所以我希望有人知道解决这个问题的好方法。

Shiny 产生这个错误:

xreg 应该是一个数值矩阵或向量

我试过了:

  • 重新安装:预测、闪亮、dplyr,并完成擦除我们的 R 和 Rstudio,并从头开始重新安装。
  • 我已经尝试将预测包回滚到 6 个月前的构建(当它工作时)
  • 我已经尝试将 R 回滚到 6 个月前的构建(当它工作时)
  • 我尝试过使用其他预测包;例如 bsts 似乎工作得很好

这是一些最低限度可行的闪亮代码。创建一个包含 3 列数据的 .csv,确保为这些列命名,y例如x1x2

 library(shiny)
 library(forecast)
 library(dplyr)


 ui <- fluidPage(

 # Application title
 titlePanel("arima test"),

 # Sidebar with a slider input for number of bins 
 sidebarLayout(
    sidebarPanel(
     
    # Input: Select a file ----
    fileInput("uploaded_file", "Choose CSV File",
              multiple = TRUE,
              accept = c("text/csv",
                         "text/comma-separated-values,text/plain",
                         ".csv")),
    
    uiOutput("dv_dropdown"),
    uiOutput("iv_checkbox")
    
  ),
  
  # Show a plot of the generated distribution
  mainPanel(
    verbatimTextOutput("model"),
    tableOutput('data')
  )
  )
  )


 server <- function(input, output) {

 # Read .csv file ----
   df <- reactive({
   req(input$uploaded_file)
   read.csv(input$uploaded_file$datapath,
         header = TRUE)

   })

 # Dynamically generate a list of the dependent variables for the user to choose from the uploaded .csv ----
   output$dv_dropdown <- renderUI({

   selectInput(inputId = "select_dependents", 
            label = "dependent time series:", 
            choices = names(df()[,c(-1)])
            )
   })

 # Dynamically generate a list of the independent variables for the user to choose ----
   output$iv_checkbox <- renderUI({

   checkboxGroupInput(inputId = "select_ivs", 
                   label = "Select Independent variables:", 
                   choices = setdiff(names(df()[,c(-1)]), input$select_dependents),
                   selected = setdiff(names(df()), input$select_dependents))
    })


 ####### CREATE REACTIVE DATA OBJECTS BASED ON FILTERS #################

 # this creates a dataframe of the dependent time series
 df_sel <- reactive({
   req(input$select_dependents)
   df_sel <- df() %>% select(input$select_dependents)


 })

 # this creates a dataframe of the dependent time series
 df_indep <- reactive({
   req(input$select_ivs)
   df_indep <- df() %>% select(input$select_ivs) 

 })


 #proof that the independent variables are in fact in a matrix/dataframe
   output$data <- renderTable({
   df_indep()
  }) 


#This is the TEST ARIMA Model 

 modela <- reactive({
 
 modela <- auto.arima(df_sel(), xreg = df_indep()) #model with regressors
 #modela <- auto.arima(df_sel()) #model without regressors to demonstrate that it actually works without the xreg argument
 })

 #this is the solution to the xreg problem above, but there's no reason I can think of that xreg shouldn't be able to take a reactive data object like it was doing before... 
 #x = as.matrix(as.data.frame(lapply(df_indep(), as.numeric))) 
 # then set: xreg = x 
 

 output$model <- renderPrint({ 
 summary(modela())
 })




 }

 # Run the application 
 shinyApp(ui = ui, server = server)
4

0 回答 0