我尝试应用经济订单quantitz
,我需要进行可视化。但是,我想在经济订单数量中指出我有重叠线的平衡点。不幸的是,
eq <- xx[which.min(abs(xx$holdingcost - xx$orderingcost)), ]
这部分不起作用。它并没有每次都指向平衡点。
这是代码。
ui.R
library(shiny)
shinyUI(pageWithSidebar(
headerPanel("Economic Order Quantity"),
sidebarPanel(
h3(strong("Parameters",style = "color:black")),
br(),
sliderInput("D", "Annual Demand Quantity (D)",min = 1, max = 1000, step= 10, value = 500),
sliderInput("S", "Ordering Cost (S)",min = 1, max = 1000, step= 1, value = 500),
sliderInput("h", "Holding Cost (i)",min = 0.01, max = 1, step= 0.01, value = 0.3),
sliderInput("C", "Cost per unit (c) ",min = 1, max = 500, step= 1, value = 150)
),
mainPanel(
plotOutput("plotgraph1"))))
server.R
library(shiny)
require(ggplot2)
require(scales)
shinyServer(function(input, output) {
output$plotgraph1<-renderPlot({
Q<-((2*input$D*input$S)/(input$h*input$C))^(1/2)
xdata <- seq(1,Q*(1.7),1)
HCOCdata <- matrix(1,1)
HCdata <- matrix(1,1)
OCdata <- matrix(1,1)
for (i in xdata) {
OC<-(input$D/i)*input$S
HC<-(i/2)*input$h*input$C
HCOC<-OC+HC
HCdata[i]<- HC
OCdata[i]<- OC
HCOCdata[i]<- HCOC
}
HCOCcost <- data.frame(HCOCcost=HCOCdata)
holdingcost <- data.frame(holdingcost=HCdata)
orderingcost <- data.frame(orderingcost=OCdata)
Quantity <- xdata
CostType <- c("HCOCcost","holdingcost","orderingcost")
xx<-cbind(Quantity,HCOCcost,holdingcost,orderingcost)
y <- ggplot(xx, aes(x=Quantity, y=Cost), group = xx , colour=xx ) +
geom_line(size=1,aes(y=HCOCcost, colour = "HCOCcost"), linetype=2) + geom_line(size=1,aes(y=holdingcost, colour = "holdingcost")) + geom_line(size=1,aes(y=orderingcost, colour = "orderingcost"))
eq <- xx[which.min(abs(xx$holdingcost - xx$orderingcost)), ]
y1 <- y + scale_y_continuous(limits=c(0, max(HCOCcost)/10),labels=comma) + geom_point(data = eq, aes(y = holdingcost), size = 4 , color="red")
y1 })})