library(shiny)

shinyServer(function(input, output) {
  output$cdf <- renderPlot({
      par(mar=c(5.1,2.1,.1,.1))

      mu <- input$CI.mu; # true mean; unknown
      sigma0 <- 1 #true sd, known
      n <- input$CI.n # sample size

      Y <- rnorm(n,mean=mu,sd=sigma0)

      alpha <- 1-input$CI.covprob
      c <- qnorm(1-alpha/2,mean=0,sd=1)

      L <- mean(Y)-c*sigma0/sqrt(n)
      U <- mean(Y)+c*sigma0/sqrt(n)

      nrep <- input$CI.nrep #number of different realisations to consider
      plot(c(mu,mu),c(0,nrep+1),xlim=c(mu-2,mu+2),col="blue",type="l",xlab=expression(mu),ylab="Different Realisations",lwd=2)
      covered <- 0
      for (i in 1:nrep){
            Y <- rnorm(n,mean=mu,sd=sigma0)

            L <- mean(Y)-c*sigma0/sqrt(n)
            U <- mean(Y)+c*sigma0/sqrt(n)
            covered <- covered+(1-(L>mu|U<mu))
            lines(c(L,U),c(i,i),lwd=2,col=ifelse(L>mu|U<mu,"red","black"))
          }
      mtext(side=1,line=4,paste(round(covered/nrep*100,1),"% of the CIs contain the true parameter value"))

    })})