我正在尝试使用新的 RStudio 功能Shiny创建一个 Web 应用程序。我正在尝试 ETS 状态空间模型。我想手动指定模型类型(server.R 中的粗体文本)。我在 server.R 的引号内给出输入。如果我们在引号内给出 iputs,它不会占用。请你帮助我好吗...
用户界面
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'))
)
)
))
服务器.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)
})