Shiny不再接受xreg值的反应函数

时间:2019-04-08 17:39:11

标签: r shiny arima forecast

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

Shiny产生此错误:“ xreg应该是数字矩阵或向量”

我尝试过: -重新安装:预测,闪亮,dplyr,并完成擦拭R和Rstudio的操作,然后从头开始重新安装。 -我曾尝试将预测包从6个月前回退到构建版本(当时该版本有效) -我曾尝试将R从6个月前回滚到版本号(当时该版本有效) -我已经尝试使用其他预测软件包;例如,bsts似乎可以正常工作

这是一些最低限度可行的闪亮代码。创建具有3列数据的.csv,请确保命名这些列,例如“ y”,“ x1”和“ x2”。

 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)

0 个答案:

没有答案