反应图中的闪亮ifelse不会改变用户输入选择

时间:2016-06-08 07:16:33

标签: r if-statement plot shiny reactive-programming

我的要求是根据用户输入更改绘图。有6个主过滤器,而且这些图分为两部分。此代码涉及第二部分 - 异常值图。根据度量类型和统计类型,除了用户选择的6个过滤器外,还应显示相应的直方图和其他统计数据。

我最初开始使用开关功能,但不知何故没有意义,所以尝试if.eelse在反应函数内。执行时,我没有得到任何错误,但是情节也没有显示。附上下面的代码段。

#Install the packages
library(shiny)
#Load the file
nx=read.csv("order_view_sample.csv",header=T,stringsAsFactors=F)
#UI code for inputs
ui=fluidPage(
titlePanel("Perf Dashboard"),
sidebarLayout(
sidebarPanel(
selectInput("RunID","Run ID:",choices=unique(as.character(nx$run_id)),multiple=T),
selectInput("OrdCat","Order Category:",choices=unique(as.character(nx$order_category)),multiple=T),
selectInput("OrdAct","Order Action:",choices=unique(as.character(nx$order_action)),multiple=T),
selectInput("OrdCon","Order Contract:",choices=unique(as.character(nx$order_contract)),multiple=T),
selectInput("OrdTyp","Order Type:",choices=unique(as.character(nx$order_types)),multiple=T),
selectInput("UsrTyp","User Type:",choices=unique(as.character(nx$user_type)),multiple=T)
),
mainPanel(
tabsetPanel(
tabPanel("Regular Graphs",selectInput("MetTyp","Metric Type:",choices=c("Ln","Thp","Both")),radioButtons("PlotTyp","Graph Type:",list("Lineplot","Barplot")),plotOutput("regplot")), 
tabPanel("Outlier Graphs",selectInput("MetTyp","Metric Type:",choices=c("Ln","Thp")),radioButtons("StatTyp","Stats Type:",list("MeanSig","MADSig")),plotOutput("outplot"))
))))
#Server code for outputs
server=function(input,output) {
mydata=reactive({
subset(input$StatTyp,nx$run_id==input$RunID & nx$order_category==input$OrdCat & nx$order_action==input$OrdAct & nx$order_contract==input$OrdCon & nx$order_types==input$OrdTyp & nx$user_type==input$UsrTyp)
})
statsType=reactive({
output$outplot=renderPlot({ 
statsType()(mydata())
if(input$StatTyp=="MeanSig") {
hist(mydata[nx$Total.Order.Confirmed],main="Mean based Thp",xlab="Thp",prob=T,border="white",xlim=c(-30,50))
curve(dnorm(x,mean(mydata),sd(mydata)),lwd=2,add=T)
abline(v=mean(mydata),col="violet",lwd=10)
abline(v=sd(mydata),col="brown",lwd=4)
abline(v=median(mydata),col="pink",lwd=5)
rect(xleft=mean(mydata)-sd(mydata),ybottom=mean(mydata)-sd(mydata),xright=mean(mydata)+sd(mydata),ytop=mean(mydata)+sd(mydata),lwd=2,lty=2,border="yellow")
rect(xleft=mean(mydata)-2*sd(mydata),ybottom=mean(mydata)-2*sd(mydata),xright=mean(mydata)+2*sd(mydata),ytop=mean(mydata)+2*sd(mydata),lwd=2,lty=2,border="orange")
rect(xleft=mean(mydata)-3*sd(mydata),ybottom=mean(mydata)-3*sd(mydata),xright=mean(mydata)+3*sd(mydata),ytop=mean(mydata)+3*sd(mydata),lwd=2,lty=2,border="red")
rect(xleft=mean(mydata)-4*sd(mydata),ybottom=mean(mydata)-4*sd(mydata),xright=mean(mydata)+4*sd(mydata),ytop=mean(mydata)+4*sd(mydata),lwd=2,lty=2,border="cyan")
rect(xleft=mean(mydata)-5*sd(mydata),ybottom=mean(mydata)-5*sd(mydata),xright=mean(mydata)+5*sd(mydata),ytop=mean(mydata)+5*sd(mydata),lwd=2,lty=2,border="blue")
rect(xleft=mean(mydata)-6*sd(mydata),ybottom=mean(mydata)-6*sd(mydata),xright=mean(mydata)+6*sd(mydata),ytop=mean(mydata)+6*sd(mydata),lwd=2,lty=2,border="green")
}else{
hist(mydata[nx$Total.Order.Confirmed],main="MAD based Thp",xlab="Thp",prob=T,border="white",xlim=c(-30,50))
curve(dnorm(x,mean(mydata),sd(mydata)),lwd=2,add=T)
abline(v=mad(mydata),col="violet",lwd=10)
abline(v=sd(mydata),col="brown",lwd=4)
abline(v=median(mydata),col="pink",lwd=5)
rect(xleft=mad(mydata)-sd(mydata),ybottom=mad(mydata)-sd(mydata),xright=mad(mydata)+sd(mydata),ytop=mad(mydata)+sd(mydata),lwd=2,lty=2,border="yellow")
rect(xleft=mad(mydata)-2*sd(mydata),ybottom=mad(mydata)-2*sd(mydata),xright=mad(mydata)+2*sd(mydata),ytop=mad(mydata)+2*sd(mydata),lwd=2,lty=2,border="orange")
rect(xleft=mad(mydata)-3*sd(mydata),ybottom=mad(mydata)-3*sd(mydata),xright=mad(mydata)+3*sd(mydata),ytop=mad(mydata)+3*sd(mydata),lwd=2,lty=2,border="red")
rect(xleft=mad(mydata)-4*sd(mydata),ybottom=mad(mydata)-4*sd(mydata),xright=mad(mydata)+4*sd(mydata),ytop=mad(mydata)+4*sd(mydata),lwd=2,lty=2,border="cyan")
rect(xleft=mad(mydata)-5*sd(mydata),ybottom=mad(mydata)-5*sd(mydata),xright=mad(mydata)+5*sd(mydata),ytop=mad(mydata)+5*sd(mydata),lwd=2,lty=2,border="blue")
rect(xleft=mad(mydata)-6*sd(mydata),ybottom=mad(mydata)-6*sd(mydata),xright=mad(mydata)+6*sd(mydata),ytop=mad(mydata)+6*sd(mydata),lwd=2,lty=2,border="green")
}
})
})
}
#Run UI and server codes
shinyApp(ui=ui,server=server)

0 个答案:

没有答案