#####UI代码:
library(shiny)
# Define UI for application that draws a histogram
fluidPage(
titlePanel("客单量与客单价对比分析"),
#h3("客单量与客单价对比分析"),
# Sidebar with controls to select the variable to plot against
# mpg and to specify whether outliers should be included
#end = as.character(Sys.Date())
sidebarLayout(
sidebarPanel(
selectInput("brand", "品牌:",
c("全部" = "全部",
"AD" = "AD",
"NK" = "NK",
"AK" = "AK",
"AO" = "AO",
"AS" = "AS",
"CV" = "CV",
"PU" = "PU",
"NF" = "NF",
"MT" = "MT",
"OT" = "OT",
"TB" = "TB",
"AC" = "AC",
"RB" = "RB",
"VN" = "VN",
"CB" = "CB",
"UG" = "UG"
)),
selectInput("regi", "大区:",
c("全部" = "全部",
"东北" = "东北",
"华北" = "华北",
"鲁豫" = "鲁豫",
"西北" = "西北",
"西南" = "西南",
"华中" = "华中",
"华东" = "华东",
"华南" = "华南",
"云贵" = "云贵"
)),
selectInput("cit", "城市:",
c("全部" = "全部"
)),
selectInput("enti", "店铺:",
c("全部" = "全部"
)),
dateRangeInput("dates",
"Date range",
start = "2015-11-01",
end = as.character(Sys.Date()),
language = "zh-CN"),
br(),
selectInput("variable", "Variable:",
c("客单量" = "quantity",
"客单价" = "price"
)), div(align="center",submitButton("确定",width='50%')),
# submitButton("确定"),
width = 3
),
# Show the caption and plot of the requested variable against
# mpg
mainPanel(
plotOutput("mpgPlot",height=450), #plotOutput("mpgPlot", height=400) br(), br(), h5(textOutput("caption"),style="color:blue")
)
)
)
#####SERVER部分代码:
library(shiny)
library(ggplot2)
library(data.table)
regiondata = fread("D:/nk4.csv",encoding="UTF-8",header=T)
head(regiondata)
#regiondata = read.csv("D:/nk4.csv",fileEncoding="UTF-8",header=T,sep=',')
regiondata=regiondata[which(regiondata$UPT>1.0),] #客单量>1.0
#uniregion<-unique(regiondata$REGION_NAME) # all regions
unibrand<-unique(regiondata$BRAND_NO) # all brands
#unibrand<-as.character(unibrand)
listname<-vector()
quantity<-vector()
price<-vector() #是否要清空???????????????
# Defien server logic required to plot various variables against
# mpg
function(input, output, session) {
output$mpgPlot <- renderPlot({
from = input$dates[1]
to = input$dates[2]
regiondata<-regiondata[which(as.Date(as.character(regiondata$PERIOD_SDATE),format="%Y%m%d") >= from & as.Date(as.character(regiondata$PERIOD_SDATE),format="%Y%m%d") <= to),]
if(input$brand=="全部") # display all brands
{
for (a in unibrand)
{
slic <- data.table(subset(regiondata,grepl(a,regiondata$BRAND_NO,ignore.case = F)))
# slic <-slic[which(slic$UPT>1.0),]
temp1<-slic$PCT
temp1=as.numeric(temp1)
temp2<-slic$UPT
temp2=as.numeric(temp2)
listname[a]<-a
quantity[a]=data.table(temp2)
price[a]=data.table(temp1)
}
}
else
{
onebrand<-input$brand
slicbrand <- data.table(subset(regiondata,grepl(onebrand,regiondata$BRAND_NO,ignore.case = F)))
uniregion<-unique(slicbrand$REGION_NAME)
if(input$regi=="全部") # display all regions
{
for (i in uniregion)
{
slic <- data.table(subset(slicbrand,grepl(i,slicbrand$REGION_NAME,ignore.case = F)))
slic <-slic[which(slic$UPT>1.0),]
temp1<-slic$PCT
temp1=as.numeric(temp1)
temp2<-slic$UPT
temp2=as.numeric(temp2)
listname[i]<-i
quantity[i]=data.table(temp2)
price[i]=data.table(temp1)
}
}
else
{
oneregion<-input$regi
slicregion <- data.table(subset(slicbrand,grepl(oneregion,slicbrand$REGION_NAME,ignore.case = F)))
head(slicregion)
unicity<-unique(slicregion$MANAGING_CITY_NAME)
if(input$cit=="全部") # display all citys of one region
{
for(j in unicity)
{
slic <- data.table(subset(slicregion,grepl(j,slicregion$MANAGING_CITY_NAME,ignore.case = F)))
slic <-slic[which(slic$UPT>1.0),]
temp1<-slic$PCT
temp1=as.numeric(temp1)
temp2<-slic$UPT
temp2=as.numeric(temp2)
listname[j]<-j
quantity[j]=data.table(temp2)
price[j]=data.table(temp1)
}
}
else
{
onecity=input$cit
sliccity <- data.table(subset(slicbrand,grepl(onecity,slicbrand$MANAGING_CITY_NAME,ignore.case = F)))
unientity<-unique(sliccity$ORGAN_NAME) uu<-vector() if(length(unientity)>20) { for(p in 1:length(unientity)) { sentity <- data.table(subset(sliccity,grepl(unientity[p],sliccity$ORGAN_NAME,ignore.case = F))) uu[p]=sum(sentity$UPT*sentity$PCT) } unientity<-unientity[order(uu,decreasing=TRUE)[1:20]] }
if(input$enti=="全部") # display all entitys of one city,one region
{
for(k in unientity)
{
slic <- data.table(subset(sliccity,grepl(k,sliccity$ORGAN_NAME,ignore.case = F)))
slic <-slic[which(slic$UPT>1.0),]
temp1<-slic$PCT
temp1=as.numeric(temp1)
temp2<-slic$UPT
temp2=as.numeric(temp2)
listname[k]<-k
quantity[k]=data.table(temp2)
price[k]=data.table(temp1)
}
}
else #display all salers of one entity
{
oneentity=input$enti
sliceentity<- data.table(subset(slicbrand,grepl(oneentity,slicbrand$ORGAN_NAME,ignore.case = F)))
unisaler<-unique(sliceentity$ASSISTANT_NAME) # all salers of one entity
for(h in unisaler)
{
slic <- data.table(subset(sliceentity,grepl(h,sliceentity$ASSISTANT_NAME,ignore.case = F)))
slic <-slic[which(slic$UPT>1.0),]
temp1<-slic$PCT
temp1=as.numeric(temp1)
temp2<-slic$UPT
temp2=as.numeric(temp2)
listname[h]<-h
quantity[h]=data.table(temp2)
price[h]=data.table(temp1)
}
}
}
}
}
if(input$variable=="quantity")
{
quantity=as.vector(quantity)
par(cex.axis=1.5,las=1,cex.main=1.7)
# if(input$regi=="regionall" ||input$regi!="regionall") par(cex.axis=1.5,las=1)
if(input$cit != "全部" ) { par(pty='m',oma=c(7,0,0,0),cex.axis=1.2,las=3)
# boxplot(quantity,main="客单量对比",xaxt = "n",xlab="") #boxplot(quantity,names = ,main="客单量对比")
# axis(1, labels = FALSE) # par(pty='m',oma=c(7,0,0,0),cex.axis=1.2)
# text(x = seq_along(listname),y = par("usr")[3]-0.05,srt=45, adj=1,labels=listname,xpd=TRUE) }
else { par(cex.axis=1.5,las=1)
}
# if(input$enti!="全部") par(cex.axis=1.5,las=1)
# par(pty='m',oma=c(4,0,0,0),las=3)
boxplot(quantity,names = listname,main="客单量对比",outline = FALSE,col='#ADD8E6')
# boxplot(quantity,main="客单量对比",xaxt = "n",xlab="",space=2) #boxplot(quantity,names = ,main="客单量对比")
# axis(1, labels = FALSE)
# text(x = seq_along(listname),y = par("usr")[3],srt=45, adj=c(1,1,1,1),labels=listname,xpd=TRUE)
# text(x = seq_along(listname),y = par("usr")[3]-1,srt=45, adj=1,labels=listname,xpd=TRUE)
}
if(input$variable=="price")
{
if(input$cit != "全部" ) par(pty='m',oma=c(4,0,0,0),cex.axis=0.8,las=3)
else par(cex.axis=1.5,las=1)
if(input$enti!="全部") par(cex.axis=1.5,las=1)
boxplot(price,names = listname,main="客单价对比",outline = FALSE)
}
})
图片是程序运行的效果图。
点击链接加入群【R语言&大数据分析】:https://jq.qq.com/?_wv=1027&k=4BQLEWp,R语言&大数据分析qq群 456726635 欢迎讨论交流
|
请发表评论