library(shiny)
shinyServer(function(input, output) {
  output$cdf <- renderPlot({
        n <- input$lr.n
        x <- 0:n
        p <- dbinom(x,prob=0.5,size=n)
        palt <- dbinom(x,prob=input$lr.palt,size=n)
        t <- ((x/n)^x*(1-x/n)^(n-x)/0.5^n)
        if (input$lr.plot=="logt"){
            t <- 2*log(t)
        }

        o <- order(t)
        t <- t[o]
        p <- p[o]
        palt <- palt[o]
        F <- stepfun(t,cumsum(c(0,p)))
        Falt <- stepfun(t,cumsum(c(0,palt)))
        par(mar=c(2.1,2.1,0.1,0.1))
        if (input$lr.plot=="logt"){
            plot(F,          ylab="",xlab="",xlim=c(0,10),main="")

            y <- seq(0,10,length.out=500)
            lines(y,pchisq(y,df=1),col="blue")
            lines(Falt,col="red")
            legend("bottomright",c("cdf of 2log(T) under H0",
                                   "cdf of chi^2_1",
                                   paste("cdf of 2log(T) if p=",input$lr.palt)
                                   ),lty=1,col=c("black","blue","red"))
        }else{
            plot(F,log="x",main="",
                 ylab="",xlab="",xlim=c(min(t),max(t)))
            lines(Falt,col="red")
            legend("bottomright",c("cdf of T under H0",
                                   paste("cdf of T if p=",input$lr.palt)
                                   ),lty=1,col=c("black","red"))        }

      })})