I'm trying to create a updatedSelectInput based on the data file that the user uploads and then plot reactive graphs while the user changes the select input. The updatedSelectInput is working fine when I upload the file, but then I can't plot the graphs based on this selection. Can anyone please help me??
ui <- fluidPage(theme = shinythemes::shinytheme("cerulean"),
titlePanel("Projecao de Demanda"),
fluidRow(
column(8,fileInput("arquivo", "Escolha o arquivo", multiple=F, accept=c(".csv")),
actionButton("ProcessarArquivo","Carregar"),
helpText("O arquivo deve conter 4 colunas (Mes e Ano, SKU, Agency e Volume) sem
cabecalho e frequencia mensal")),
column(4,selectInput("SelectSKU",label = h5("Filtrar por SKY"),"")
),
fluidRow(
column(6,plotOutput("GraficoHistorico")),
column(6,plotOutput("DecomposicaoTS"))
),
fluidRow(
column(12,numericInput("MesesAProjetar","Meses a Projetar",12 , min=1, max=60),
actionButton("Processar","Processar"),
plotOutput("GraficoProjetado"))),
fluidRow(
column(12,tableOutput("ResultadoProjetado")))
))
server <- function(input, output, session) {
inFile <- reactive({
if (is.null(input$arquivo))
{return(NULL)} else
{input$arquivo}
})
data <- reactive({
if (is.null(inFile()))
{return(NULL)} else
{read.csv2(inFile()$datapath, header = F, stringsAsFactors=FALSE)}
})
observe({
if (is.null(data()))
{return(NULL)} else
updateSelectInput(session,"SelectSKU",choices=unique(data()$V3), selected=NULL)
})
observe({
if (is.null(input$SelectSKU))
{return(NULL)} else
filter(data(),data()$V3==input$SelectSKU)
data()$V4 <- as.numeric(data()$V4)
volume_per_month = aggregate(data()$V4, by=list(data()$V1), FUN = sum)
names(volume_per_month)[1] <- "YearMonth"
names(volume_per_month)[2] <- "TotalVolume"
as.Date(volume_per_month$YearMonth, "%d/%m/%Y")
library(forecast)
vlr = input$MesesAProjetar
myts <- ts(volume_per_month$TotalVolume,start=c(2013,1), end=c(2017,12), frequency = 12)
output$GraficoHistorico = renderPlot({plot.ts(myts,plot.type="m", col="blue", lwd=2, main = "Historico de Vendas", xlab="Anos", ylab="Volume de Vendas")})
output$DecomposicaoTS = renderPlot({autoplot({decompose(myts)}, main="Decomposição")})
arima = auto.arima(myts,D=1)
previsao = forecast(arima,h=12)
output$GraficoProjetado = renderPlot({plot(previsao, main="Previsao para os proximos meses")})
output$ResultadoProjetado = renderTable({(previsao)},rownames=TRUE)
})}
shinyApp(ui = ui, server = server)