library(shiny)

shinyServer(function(input, output) {

  output$cdf <- renderPlot({
    set.seed(input$bayes.seed)
    theta <- rnorm(1)
    X <- rnorm(input$bayes.n,mean=theta)
    t <- seq(-3,3,length.out=501)
    par(mar=c(2.1,2.1,6.1,0.1))
    plot(t,dnorm(t),xlim=c(-3,3),ylim=c(0,3),type="l",xlab="",ylab="")
    Xtxt <- list
    if (length(X)<=30){
      Xtxt <- paste(round(X,2),collapse=",")
    }else{
      Xtxt <- paste(paste(round(X[1:30],2),collapse=","),",...",sep="")
    }

    mtext(expression(paste("Observations ",X[1],",...,",X[n],":")),line=5,at=-3,adj=0)
    Xtmp <- X;
    pos <- 4;
    while(length(Xtmp)>0 &&pos>=0){
      Xakt <- Xtmp[1:min(length(Xtmp),25)]
      mtext(paste(round(Xakt,2),collapse=","),line=pos,at=-3,adj=0)
            Xtmp <- Xtmp[-(1:length(Xakt))]
      pos <- pos-1
    }
    lines(t,dnorm(t,mean=sum(X)/(length(X)+1),sd=sqrt(1/(length(X)+1))),col="blue")
    legend("topleft",c("a-priori density","a-posteriori density"),lty=1,col=c("black","blue"))
  })
})