Когда я нажимаю на Фильтр, т. Е. Area Office, он работает нормально без обновления. Но после обновления таблицы и нажатия кнопки сброса в следующий раз, когда я выбираю фильтр, он показывает ошибку. Почему это происходит, я не знаю. Здесь я хочу сначала отфильтровать данный набор данных по районному офису, затем обновить значение и затем сбросить его для запуска fre sh
CODE
library(shiny)
library(shinydashboard)
library(semantic.dashboard)
library(DT)
library(ggplot2)
library(shinyjs)
library(plotly)
library(lubridate)
library(plyr)
library(dplyr)
library(zoo)
sales <- read.csv("sales.csv")
sales=arrange(sales,Date)
sales$Date=as.yearmon(sales$Date)
ui=dashboardPage(title = "Price Elasticity Analysis",
dashboardHeader(color = "red",title = "Elasticity Analysis",inverted = TRUE),
dashboardSidebar( color = "blue",
sidebarMenu(
selectInput("ao","Area Office",
c("MUMBAI","DELHI","CHENNAI","KOLKATA","SURAT","PAN INDIA")
,selected = c("PAN INDIA"),selectize = FALSE),
p(),
sliderInput("price","%change",min=-50,max=50,value=0,step=1,post = '%'),
tags$style("#price {width:90px;}"),
p(),
textInput("brand", "BrandPrice",value=0),
tags$style("#brand {width:90px;vertical-align: middle;}"),
p(),
textInput("competition","Competitor Price",value=0),
tags$style("#competition {width:90px;}"),
p(),
textInput("branddx","Brand DX",value=0),
tags$style("#branddx {width:90px;}"),
p(),
textInput("Brandnondx","Brand Non DX",value=0),
tags$style("#Brandnondx {width:90px;}"),
p(),
sliderInput("diwali","Diwali",min=0,max=1,value=0,step=1),
tags$style("#diwali {width:90px;}"),
p(),
textInput("crude","Crude oil Price",value=0),
tags$style("#crude {width:90px;}"),
p(),
textInput("inflation","Inflation",value=0),
tags$style("#inflation {width:90px;}"),
p(),
textInput("consumer","Consumer Non Durable",value=0),
tags$style("#consumer {width:90px;}"),
p(),
textInput("primary","Primary Goods",value=0),
tags$style("#primary {width:90px;}"),
p(),
textInput("atl","ATL Ratio",value=0),
tags$style("#atl {width:90px;}"),
p(),
textInput("total","Total Spend",value=0),
tags$style("#total {width:90px;}"),
p(),
actionButton("update", "Update Table"),
p(),
actionButton("reset", "Reset"),
hr(),
id="form",
useShinyjs()
),
disable = FALSE,collapsed=FALSE,
pushable = TRUE,center = TRUE
),
dashboardBody(width=300,
tabItems(
tabItem("dashboard",
fluidRow(
column( width= 8,h3('Latest Values'),
dataTableOutput('table')
),
column(width=6,h3("Graphs"),
fluidRow(
plotOutput("plot_y",width = "70%"),
plotOutput("plot_z",width = "70%")
))
)
)
)
),theme = "journal"
)
server=function(input, output, session) {
values <- reactiveValues()
values$df <- data.frame(
Brandprice=sales$Brandprice,
Competition=sales$Competition,
Brand_DX= sales$Brand_DX,
BRANDX_Non_MnM_Comp_Disct= sales$BRANDX_Non_MnM_Comp_Disct,
Diwali= sales$Diwali_Flag,
Crude_Oil_price= sales$Crude_Oil,
Inflation= sales$Inflation,
IIP_Consumer_Non_durables= sales$IIP_Consumer_Non_durables,
Primary_Goods= sales$Primary_Goods,
BRANDX_Non_MnM_Comp_ATL_Ratio= sales$BRANDX_Non_MnM_Comp_ATL_Ratio,
BRANDX_TOTAL_SPEND_ADS_30_70_MA3= sales$BRANDX_TOTAL_SPEND_ADS_30_70_MA3,
AO= sales$AO,
Sales= sales$Sales,
Date= sales$Date
)
f=
newEntry <-
observe({
if(input$update > 0) {
newLine <- isolate(c
(as.numeric(input$brand)+(as.numeric(input$brand)*as.numeric(input$price))/100,
as.numeric(input$competition)+(as.numeric(input$competition)*as.numeric(input$price))/100,
as.numeric(input$branddx)+(as.numeric(input$brand)*as.numeric(input$price))/100,
as.numeric(input$Brandnondx)+(as.numeric(input$Brandnondx)*as.numeric(input$price))/100,
input$diwali,
as.numeric(input$crude)+(as.numeric(input$crude)*as.numeric(input$price))/100,
as.numeric(input$inflation)+(as.numeric(input$inflation)*as.numeric(input$price))/100,
as.numeric(input$consumer)+(as.numeric(input$consumer)*as.numeric(input$price))/100,
as.numeric(input$primary)+(as.numeric(input$primary)*as.numeric(input$price))/100,
as.numeric(input$atl)+(as.numeric(input$atl)*as.numeric(input$price))/100,
as.numeric(input$total)+(as.numeric(input$total)*as.numeric(input$price))/100,
input$ao,
(1.043-.0000018*(as.numeric(input$brand)+
(as.numeric(input$brand)*as.numeric(input$price))/100)-
.0000031*(as.numeric(input$competition)+(as.numeric(input$competition)*as.numeric(input$price))/100)+
.0000735*(as.numeric(input$branddx)+(as.numeric(input$branddx)*as.numeric(input$price))/100)+
.0000143*(as.numeric(input$Brandnondx)+(as.numeric(input$Brandnondx)*as.numeric(input$price))/100)+
.322*(as.numeric(input$diwali))+
.01837*(as.numeric(input$crude)+(as.numeric(input$crude)*as.numeric(input$price))/100)+
.05033*(as.numeric(input$inflation)+(as.numeric(input$inflation)*as.numeric(input$price))/100)+
.008243*(as.numeric(input$consumer)+(as.numeric(input$consumer)*as.numeric(input$price))/100)+
.04247*(as.numeric(input$primary)+(as.numeric(input$primary)*as.numeric(input$price))/100)+
.1013*(as.numeric(input$atl)+(as.numeric(input$atl)*as.numeric(input$price))/100)+
.08477*(as.numeric(input$total)+(as.numeric(input$brand)*as.numeric(input$total))/100))
)
)
if(input$ao=="MUMBAI"){
isolate(values$df <- rbind(subset(values$df,AO=="MUMBAI"), newLine))
}
else if (input$ao=="DELHI"){
isolate(values$df <- rbind(subset(values$df,AO=="DELHI"), newLine))
}
else if (input$ao=="KOLKATA"){
isolate(values$df <- rbind(subset(values$df,AO=="KOLKATA"), newLine))
}
else if (input$ao=="CHENNAI"){
isolate(values$df <- rbind(subset(values$df,AO=="CHENNAI"), newLine))
}
else if (input$ao=="SURAT"){
isolate(values$df <- rbind(subset(values$df,AO=="SURAT"), newLine))
}
else if(input$ao=="PAN INDIA") {
isolate(values$df <- rbind(subset(values$df,AO=="PAN INDIA"), newLine))
}
}
else if(input$update==0){
if(input$ao=="MUMBAI"){
isolate(values$df <- subset(sales,AO=="MUMBAI"))
}
else if (input$ao=="DELHI"){
isolate(values$df <- subset(sales,AO=="DELHI"))
}
else if (input$ao=="KOLKATA"){
isolate(values$df <- subset(sales,AO=="KOLKATA"))
}
else if (input$ao=="CHENNAI"){
isolate(values$df <- subset(sales,AO=="CHENNAI"))
}
else if (input$ao=="SURAT"){
isolate(values$df <- subset(sales,AO=="SURAT"))
}
else {
isolate(values$df <-subset(sales,AO=="PAN INDIA"))
}
}
}
)
#plotting of crossprice elasticity
output$plot_z= renderPlot({
ggplot(data=values$df, aes(y=Sales, x=Competition,group=1)) +
geom_point(size=2,colour="#CC0000") +ggtitle("cross-price elasticity")+geom_smooth(method='loess')+xlab("price") +
ylab("sales")
}, height = 350, width = 500)
#plotting of price elasticity
output$plot_y= renderPlot({
ggplot(data = values$df,aes(y=Sales,x=Brandprice,group=1))+ geom_point(size=2,colour="#CC0000") +geom_smooth(method='loess')+xlab("price") +
ylab("sales") +
ggtitle("Price Elasticity")
}, height = 350, width = 500)
#printing the values
s=function(c,d,e){
c=mean(as.numeric(values$df[,1]))
d= mean(as.numeric(values$df[,13]))
e= mean(as.numeric(values$df[,2]))
s1= (-0.0000018)*(c/d)
s2= (-0.0000031)*(e/d)
s3=round(as.numeric(values$df[length(as.numeric(values$df[,1])),13]))
s4= ((as.numeric(values$df[length(as.numeric(values$df[,1])),13])-
as.numeric(values$df[length(as.numeric(values$df[,1]))-1,13]))/
as.numeric(values$df[length(as.numeric(values$df[,1]))-1,13]))*100
s5= ((as.numeric(values$df[length(as.numeric(values$df[,1])),1])-
as.numeric(values$df[length(as.numeric(values$df[,1]))-1,1]))/
as.numeric(values$df[length(as.numeric(values$df[,1]))-1,1]))*100
s6= ((as.numeric(values$df[length(as.numeric(values$df[,1])),2])-
as.numeric(values$df[length(as.numeric(values$df[,1]))-1,2]))/
as.numeric(values$df[length(as.numeric(values$df[,1]))-1,2]))*100
res= c(s1,s2,s3,s4,s5,s6)
}
#printing the table
output$table = DT::renderDataTable({
DT::datatable(t(cbind(values$df[length(as.numeric(values$df[,1])),],
sales_growth= paste(round(s(c,d,e)[4],2),"%"),
Mahindra_Pricechange =paste(round(s(c,d,e)[5],2),"%"),
competitor_Pricechange= paste(round(s(c,d,e)[6],2),"%"),
pelasticity=s(c,d,e)[1],
cpelasticity=s(c,d,e)[2]
))
, options = list(pageLength=19))
}
)
observe({ if(input$reset!=0) {
if(input$update > 0) {
if(input$ao=="MUMBAI"){
isolate(sales <- subset(sales,AO=="MUMBAI"))
}
else if (input$ao=="DELHI"){
isolate(sales <- subset(sales,AO=="DELHI"))
}
else if (input$ao=="KOLKATA"){
isolate(sales <- subset(sales,AO=="KOLKATA"))
}
else if (input$ao=="CHENNAI"){
isolate(sales <- subset(sales,AO=="CHENNAI"))
}
else if (input$ao=="SURAT"){
isolate(sales <- subset(sales,AO=="SURAT"))
}
else {
isolate(sales <-subset(sales,AO=="PAN INDIA"))
}
#plotting of crossprice elasticity
output$plot_z= renderPlot({
ggplot(data=sales, aes(y=sales$Sales, x=sales$Competition,group=1)) +
geom_point(size=2,colour="#CC0000") +ggtitle("cross-price elasticity")+xlab("price") +
ylab("sales") +
geom_smooth(method='loess') }, height = 350, width = 500)
#plotting of price elasticity
output$plot_y= renderPlot({
ggplot(data = sales,aes(y=sales$Sales,x=sales$Brandprice,group=1))+ geom_point(size=2,colour="#CC0000") +
xlab("price") +
ylab("sales") + ggtitle("Price Elasticity")+ geom_smooth(method='loess')
}, height = 350, width = 500)
#printing the values
s=function(a,b,c){
a=mean(as.numeric(sales$Brandprice))
b= mean(as.numeric(sales$Sales))
c= mean(as.numeric(sales$Competition))
s1= (-0.0000018)*(a/b)
s2= (-0.0000031)*(c/b)
s3=round(as.numeric(values$df[length(as.numeric(values$df[,1]))-1,1]))
s4= ((sales[as.numeric(length(sales[,1])),13]-sales[as.numeric(length(sales[,1]))-1,13])
/sales[as.numeric(length(sales[,1]))-1,13])*100
s5= ((sales[as.numeric(length(sales[,1])),1]-sales[as.numeric(length(sales[,1]))-1,1])
/sales[as.numeric(length(sales[,1]))-1,1])*100
s6= ((sales[as.numeric(length(sales[,1])),2]-sales[as.numeric(length(sales[,1]))-1,2])
/sales[as.numeric(length(sales[,1]))-1,2])*100
res= c(s1,s2,s3,s4,s5,s6)
}
#printing the table
output$table = DT::renderDataTable({
DT::datatable(t(cbind(sales[as.numeric(length(sales[,1])),],sales_growth=paste(round(s(a,b,c)[4],2),"%"),
mahindra_Pricechange =paste(round(s(a,b,c)[5],2),"%"),
competitor_pricechange =paste(round(s(a,b,c)[6],2),"%"),
pelasticity=s(a,b,c)[1],
cpelasticity=s(a,b,c)[2]))
, options = list(pageLength=19)
)
}
)
}
}
}
)
observeEvent(input$reset, {
reset("form")
})
}
shinyApp(ui,server)
НАБОР ДАННЫХ
введите описание изображения здесь