ETS model using Shiny

606 views Asked by At

Im trying to create a web application with the new RStudio feature Shiny. I'm trying ETS state space model. I want to specify model type manually (Bold text in server.R). I'm giving inputs within quotes in server.R. If we give iputs within quotes it will not take. Could you please help me...

ui.R

library(shiny)
shinyUI(pageWithSidebar(
  headerPanel( "Forecast", "Flowserve"),
  sidebarPanel(
    fileInput('file1', 'Select csv file',
              accept=c('text/csv')
              ),
    checkboxInput('header', 'Header', TRUE),
    radioButtons('sep', 'Separator',
                 c(Comma=',', Semicolon=';', Tab='\t')
                 ),
    tags$hr(),
    numericInput("startyear", "Start Year and Month",2010),
    sliderInput("month","",min=1, max=12,value=1, step=1, animate=T),

    tags$hr(),
    selectInput("error", "Error Type", list("Multiplicative"="M","Additive"="A")),
    selectInput("trend", "Trend Type", list("Multiplicative"="M","Additive"="A", "Null"="N")),
    selectInput("seasonal", "Seasonal Type", list("Multiplicative"="M","Additive"="A", "Null"="N")),
  submitButton("UPDATE")
  ),

  mainPanel(

    tabsetPanel(
    tabPanel("Data", tableOutput('contents')),
    tabPanel("Time Plot",  plotOutput('tsplot')),
    tabPanel("Forecast",  plotOutput('plotforecast'))
            )
    )
))

server.R

library(shiny)
library(forecast)
shinyServer(function(input,output){

  data1 = reactive({
  inFile<-input$file1
  if(is.null(inFile))
    return(NULL)
  read.csv(inFile$datapath, header=input$header, sep=input$sep)
  })
output$plotforecast<-renderPlot(function(){
  datats<-ts(data1(), start=c(input$startyear,input$month), frequency=12)
  model<-ets(datats, model="input$error input$trend input$seasonal")
  fit1<-fitted(model)
  future1<-forecast(model, h=4, level=c(95,97.5))  
  p2<-plot.forecast(future1,shadecols=c("yellow","orange"), xlab=expression(bold(Year)), ylab=expression(bold(Demand)))
  print(p2)
  })
1

There are 1 answers

0
sgibb On

You should replace model<-ets(datats, **model="input$error input$trend input$seasonal"**) by model<-ets(datats, model=paste0(input$error, input$trend, input$seasonal)) because the model argument expect a 3-letter-string. paste0 concatenates character vectors (see ?paste0 for details).