Reputation: 27
I am trying to create a shiny app to accomplish the following -
Sample structure of code to give you an idea of how i am trying to meet these requirements -
Server.R:
#Excerpt of server code
branch_data <- openxlsx::read.xlsx("Branch_Final.xlsx")
<Other data input and cleaning code>
branch_data_final <- data.table(branch_data)
shinyServer(function(input, output) {
#Filtering data set using ID (input$select_ID is the variable)
data_branch_analysis<-(data_branch_analysis[ID==input$select_ID])[order(DATE)]
#Data manipulation for regression
data_branch_analysis[,NDATE:=as.Date(DATE,"%Y.%m.%d")]
data_branch_analysis[,L_AVG_AGE:=shift(AVG_AGE,1)]
data_branch_analysis[,L_AVG_WAGE:=as.numeric(shift(AVG_WAGE,1))]
<Other lines of code for manipulation>
#Regression
fit1<-lm(data=data_branch_analysis,VISITOR_NUM~0+time+WD+L_W3_7+L_W7_14+L_W14_21+...)
bestm<-step(fit1)
fit2<-auto.arima(data_branch_analysis_train$VISITOR_NUM,max.order=30,xreg=as.matrix(x_reg))
<Other lines of code for regression)
#GGPLOT
output$final_forecast_branch <- renderPlot({
g <-
ggplot()+geom_line(aes(x=data_branch_analysis$NDATE,y=data_branch_analysis$VISITOR_NUM,col="original"))+
geom_line(aes(x=data_branch_analysis$NDATE[2:(ntrain+1)],y=fit2$fitted,col="train"))+
geom_line(aes(x=data_branch_analysis$NDATE[(ntrain+2):nrow(data_branch_analysis)],y=fore2$mean,col="test"))
g
})
output$final_forecast_branch_analysis_accuracy <- renderText(expr = accuracy(fore2,x=data_branch_analysis_$VISITOR_NUM[(ntrain+2):nrow(data_branch_analysis)])
}
UI.R
#Excerpt of UI code
navbarMenu("Analyzer Widget",
tabPanel(
"Branch",
sidebarLayout(
fluid = 'TRUE',
sidebarPanel(
# p("Please enter the following information - "),
selectInput(
inputId = "select_ID",
'Select Branch ID',
selected = "106841",
sort(unique(data_branch_analysis$ID))
)
),
mainPanel(tabsetPanel(
tabPanel(
'Training Data',
plotOutput('final_forecast_branch'),
p("Accuracy of Model"),
textOutput("final_forecast_branch_analysis_accuracy"),
...
Currently, i see no output from the ggplot or textOutput blocks. I've tried reactive and observe but obviously i am unable to implement in properly. Would appreciate your thoughts on how to structure the code for this to work.
Thanks for your inputs.
Upvotes: 0
Views: 641
Reputation: 5003
Hi you ewant to make a chain of reactive expressions someting like my example below.
shinyServer(function(input, output) {
filterDta <- reactive({
#Filtering data set using ID (input$select_ID is the variable)
data_branch_analysis<-(data_branch_analysis[ID==input$select_ID])[order(DATE)]
})
minpulateDta <- reactive({
data_branch_analysis <- filterDta()
#Data manipulation for regression
data_branch_analysis[,NDATE:=as.Date(DATE,"%Y.%m.%d")]
data_branch_analysis[,L_AVG_AGE:=shift(AVG_AGE,1)]
data_branch_analysis[,L_AVG_WAGE:=as.numeric(shift(AVG_WAGE,1))]
<Other lines of code for manipulation>
})
calcRegression <- reactive({
#Regression
data_branch_analysis <- minpulateDta()
fit1<-lm(data=data_branch_analysis,VISITOR_NUM~0+time+WD+L_W3_7+L_W7_14+L_W14_21+...)
bestm<-step(fit1)
fit2<-auto.arima(data_branch_analysis_train$VISITOR_NUM,max.order=30,xreg=as.matrix(x_reg))
<Other lines of code for regression)
})
#GGPLOT
output$final_forecast_branch <- renderPlot({
data_branch_analysis <- calcRegression()
g <-
ggplot()+geom_line(aes(x=data_branch_analysis$NDATE,y=data_branch_analysis$VISITOR_NUM,col="original"))+
geom_line(aes(x=data_branch_analysis$NDATE[2:(ntrain+1)],y=fit2$fitted,col="train"))+
geom_line(aes(x=data_branch_analysis$NDATE[(ntrain+2):nrow(data_branch_analysis)],y=fore2$mean,col="test"))
g
})
output$final_forecast_branch_analysis_accuracy <- renderText(expr = accuracy(fore2,x=data_branch_analysis_$VISITOR_NUM[(ntrain+2):nrow(data_branch_analysis)])
}
In this case of course you don't really need to seperate minpulateDta
and calcRegression
but it makes the code more readable when you separete the different steps. If you want to reuse the results some where else it is also easier in this way.
Hope this helps!
Upvotes: 1