shinyUI(fluidPage(
  tabsetPanel(
    tabPanel("Plot",
             fluidRow(
               column(4,
                      tabsetPanel(
                        tabPanel("Treatment",
                                 br(),
                                 sliderInput("tfd1", "time of first dose:", value=8, min=0, max = 20, step=2),
                                 sliderInput("nd1", "number of doses:", value=4, min=0, max = 20, step=1),
                                 sliderInput("ii1", "interdose interval:", value = 4, min = 1, max = 10, step=1),
                                 sliderInput("amt1", "amount:", value = 30, min = 0, max = 100, step=5),
                                 br()
                        ),
                        tabPanel("Parameters",
                                 fluidRow(
                                   column(6,
                                          sliderInput('V1', label='V1', min=0.4, max=2, value=0.8, step=0.16),
                                          sliderInput('k1', label='k1', min=0.5, max=2, value=1, step=0.15),
                                          sliderInput('k2', label='k2', min=0.3, max=1, value=0.6, step=0.07),
                                          sliderInput('k10', label='k10', min=10, max=40, value=20, step=3),
                                          sliderInput('k12', label='k12', min=0.07, max=0.3, value=0.1, step=0.023),
                                          sliderInput('k2', label='k2', min=0.3, max=1, value=0.6, step=0.07),
                                          br()
                                   ),
                                   column(6,
                                          sliderInput('k21', label='k21', min=1, max=4, value=2, step=0.3),
                                          sliderInput('lambda0', label='lambda0', min=0.1, max=0.5, value=0.3, step=0.04),
                                          sliderInput('lambda1', label='lambda1', min=0.4, max=2, value=0.8, step=0.16),
                                          sliderInput('psi', label='psi', min=10, max=40, value=20, step=3),
                                          sliderInput('w0', label='w0', min=0.03, max=0.1, value=0.06, step=0.007)
                                   ))
                        )
                      )),
               column(8,
                      plotOutput("plot",  height="450px"))
             )),
#     tabPanel("Mlxtran", pre(includeText("simeoni.txt"))),
    tabPanel("ui.R", pre(includeText("ui.R"))),
    tabPanel("server.R", pre(includeText("server.R")))
  )
))
source("../../initMlxR.R")

shinyServer(function(input, output) {

  r <- reactive({  
    param.value <-c(input$V1,input$k1,input$k2,input$k10,input$k12,input$k2,
                    input$k21,input$lambda0,input$lambda1,input$psi,input$w0)   
    f  <- list(name='W',time=seq(0,50,by=0.2))
    p   <- list(name=c('V1', 'k1', 'k2', 'k10', 'k12', 'k2', 'k21', 
                       'lambda0', 'lambda1', 'psi', 'w0'), 
                value=param.value)
    t11=input$tfd1
    t12=input$ii1*(input$nd1-1)+t11
    if (t12>=t11){
      t1.dose=seq(t11,t12,by=input$ii1)
      adm1 <- list(time=t1.dose, amount=input$amt1, type=1)
    }else{
      adm1 <- list(time=t11, amount=0, type=1)
    }

    res <- simulx( model     = 'simeoni.txt',
                   parameter = p,
                    treatment = adm1,
                   output    = f)
    return(res)
  })  
  
  output$plot <- renderPlot({
      r=r()
    pl=ggplotmlx(data=r$W, aes(x=time, y=W)) + geom_line(size=0.75)
    print(pl)
  })
})