Hello @FJCC I am here with another problem. Sorry for bothering you so much. Actually I have to optimize a server.R code. Lets see the code first.
library(shiny)
library(tsbox)
library(fpp2)
library(xts)
library(zoo)
library(lubridate)
library(gdata)
shinyServer(function(input,output,session){
tsData <- reactive({
validate(
need(input$file != "", "Please select a data set")
)
file_in <- input$file
x <- file_in$datapath
datf = read.csv(x,header = TRUE, stringsAsFactors = FALSE)
datf[,1] = as.character(datf[,1])
datf[,2] <- as.numeric(datf[,2])
if (sum(is.na(dmy(datf[,1]))) == 0) datf[,1] = dmy(datf[,1])
else if (sum(is.na(mdy(datf[,1]))) == 0) datf[,1] = mdy(datf[,1])
else if (sum(is.na(ymd(datf[,1]))) == 0) datf[,1] = ymd(datf[,1])
else if (sum(is.na(my(datf[,1]))) == 0) datf[,1] = my(datf[,1])
else if (sum(is.na(ym(datf[,1]))) == 0) datf[,1] = ym(datf[,1])
else if (sum(is.na(dm(datf[,1]))) == 0) datf[,1] = dm(datf[,1])
else if (sum(is.na(md(datf[,1]))) == 0) datf[,1] = md(datf[,1])
datfXts<- xts(datf[,2], order.by = datf[,1])
datf_ts <- ts_ts(datfXts)
return(datf_ts)
})
columnNames<-reactive({
file_in <- input$file
columns <- colnames(read.csv(file_in$datapath,header = TRUE, stringsAsFactors = FALSE))
if(startsWith(columns[1], 'X', trim=TRUE) && startsWith(columns[2], 'X', trim=TRUE) ){
columns[1] = "Time"
columns[2] = "Value"
}
return(columns)
})
output$plot1 <- renderPlot({
datf_ts<-tsData()
columns <- columnNames()
autoplot(datf_ts, series = "Original Data") + xlab(columns[1]) + ylab(columns[2])
})
output$plot2 <- renderPlot({
datf_ts<-tsData()
datf_ts %>% auto.arima() %>% checkresiduals()
})
output$plot3 <- renderPlot({
datf_ts<-tsData()
columns <- columnNames()
datf_ts %>% auto.arima() %>% fitted() %>% autoplot(series = "Fitted Data") + autolayer(datf_ts, series = "Original Data") + xlab(columns[1]) + ylab(columns[2])
})
output$plot4 <- renderPlot({
datf_ts<-tsData()
columns <- columnNames()
datf_ts %>% auto.arima() %>% forecast() %>% autoplot(series = "Origina + Forecast") + xlab(columns[1]) + ylab(columns[2])
})
})
Actually the reactive functions are called four times after uploading every file. It should be only one call . I am having error with global variables. How can I do it?
Thanks in advance.