library(shiny)

shinyServer(function(input, output,session) {


  output$cdf <- renderPlot({
    n <- input$n
    p <- input$p
    par(mar=c(2.1,2.1,.1,.1))
    if (input$dist=="MLE"){
      x <- seq(-0.001,1.001,by=0.001)
      plot(x,pbinom(x*n, prob=p, size=n),type="l",col="blue",lwd=2,xlab="",ylab="")
      legend("topleft",title="cdf of",expression(hat(p)[n]),col=c("blue"),lwd=2)
    }
    else{
      x <- seq(-2,2,by=0.01)
      plot(x,pnorm(x,sd=sqrt(p*(1-p))),type="l",main="",lwd=2,ylab="",xlab="")
      lines(x,pbinom(x*sqrt(n)+n*p, prob=p, size=n),col="blue",lwd=2)
      legend("topleft",title="cdf of",expression(N(0,p*(1-p)),
          sqrt(n)*(hat(p)[n]-p)),col=c("black","blue"),lwd=2)
    }
},height = function() {
      session$clientData$output_cdf_width
  },
                           width=function() {
                               session$clientData$output_cdf_width
                           }
                           )
}
            )