type = 1


type = 2




[LONGITUDINAL]
input={IC50,kaA,kaB,k21,FV1A,FV1B,psi,lambda0,lambda1,k1,k2,w0,keA,Emax,k12,keB,IC50c}

PK:
depot(type=1, target=Q0A)
depot(type=2, target=Q1B)

EQUATION:
Q0A_0 = 0
Q1A_0 = 0
c1A = FV1A*Q1A
Q0B_0 = 0
Q1B_0 = 0
Q2B_0 = 0
c1B = FV1B*Q1B
Z0_0 = w0
Z1_0 = 0
Z2_0 = 0
Z3_0 = 0
W = Z0 + Z1 + Z2 + Z3
k2h = k2*(1-c1A/(IC50c+c1A))
ddt_Q0A = -kaA*Q0A
ddt_Q1A = kaA*Q0A -keA*Q1A
ddt_Q0B = -kaB*Q0B
ddt_Q1B = kaB*Q0B-(k12+keB)*Q1B + k21*Q2B
ddt_Q2B = k12*Q1B - k21*Q2B
ddt_Z0 = lambda0*Z0/((1+(((lambda0/lambda1)*W)^psi))^(1/psi))*(1-Emax*c1A/(IC50+c1A)) - k2h*c1B*Z0
ddt_Z1 = k2h*c1B*Z0 - k1*Z1
ddt_Z2 = k1*Z1 - k1*Z2 
ddt_Z3 = k1*Z2 - k1*Z3 
shinyUI(fluidPage(
  tabsetPanel(
    tabPanel("Plot",
        fluidRow(
          column(4,
            tabsetPanel(
              tabPanel("Treatment",
                       fluidRow(
                         column(6,
                                br(),
                                h5("type = 1"),
                                sliderInput("tfd1", "time of first dose:", value=8, min=0, max = 40, step=4),
                                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 = 0.2, min = 0, max = 0.5, step=0.05),
                                br()
                         ),
                         column(6,
                                br(),
                                h5("type = 2"),
                                sliderInput("tfd2", "time of first dose:", value=9, min=0, max = 40, step=3),
                                sliderInput("nd2", "number of doses:", value=10, min=0, max = 20, step=1),
                                sliderInput("ii2", "interdose interval:", value = 1, min = 0.5, max = 5, step=0.5),
                                sliderInput("amt2", "amount:", value = 80, min = 0, max = 200, step=20),
                                br()
                         ))),
              tabPanel("Parameters",
                    fluidRow(
                     column(6,
                       sliderInput('Emax', label='Emax', min=0.5, max=2, value=1, step=0.15),
                       sliderInput('FV1A', label='FV1A', min=4, max=20, value=8, step=1.6),
                       sliderInput('FV1B', label='FV1B', min=0.2, max=0.9, value=0.5, step=0.07),
                       sliderInput('IC50', label='IC50', min=2, max=7, value=4, step=0.5),
                       sliderInput('IC50c', label='IC50c', min=1, max=4, value=2, step=0.3),
                       sliderInput('k1', label='k1', min=2, max=7, value=4, step=0.5),
                       sliderInput('k12', label='k12', min=70, max=300, value=100, step=23),
                       sliderInput('k2', label='k2', min=0.1, max=0.4, value=0.2, step=0.03),
                       sliderInput('k21', label='k21', min=5, max=20, value=10, step=1.5),
                       br()
                     ),
                     column(6,
                       sliderInput('kaA', label='kaA', min=1, max=5, value=3, step=0.4),
                       sliderInput('kaB', label='kaB', min=9, max=40, value=20, step=3.1),
                       sliderInput('keA', label='keA', min=0.06, max=0.2, value=0.1, step=0.014),
                       sliderInput('keB', label='keB', min=20, max=100, value=50, step=8),
                       sliderInput('lambda0', label='lambda0', min=0.07, max=0.3, value=0.1, step=0.023),
                       sliderInput('lambda1', label='lambda1', min=0.06, max=0.3, value=0.1, step=0.024),
                       sliderInput('psi', label='psi', min=1, max=20, value=5, step=1),
                       sliderInput('w0', label='w0', min=0.03, max=0.1, value=0.06, step=0.007),
                       br()
                     )))
                )),
       column(8,
          plotOutput("plot",  height="500px"))
       )),
    tabPanel("Mlxtran", pre(includeText("rocchetti.txt"))),
    tabPanel("ui.R", pre(includeText("ui.R"))),
    tabPanel("server.R", pre(includeText("server.R")))
  )
))
my.dir=getwd()
source("../../initMlxR.R")
setwd(my.dir)

shinyServer(function(input, output) {

  r <- reactive({  
    param.value <-c(input$Emax,input$FV1A,input$FV1B,input$IC50,input$IC50c,
                    input$k1,input$k12,input$k2,input$k21,input$kaA,input$kaB,
                    input$keA,input$keB,input$lambda0,input$lambda1,input$psi,input$w0)   
    f  <- list(name='W',time=seq(0,50,by=0.2))
    p   <- list(name=c('Emax', 'FV1A', 'FV1B', 'IC50', 'IC50c', 'k1', 'k12', 'k2', 'k21', 
                       'kaA', 'kaB', 'keA', 'keB', '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)
    }
    t21=input$tfd2
    t22=input$ii2*(input$nd2-1)+t21
    if (t22>=t21){
      t2.dose=seq(t21,t22,by=input$ii2)
      adm2 <- list(time=t2.dose, amount=input$amt2, type=2)
    }else{
      adm2 <- list(time=t21, amount=0, type=2)
    }

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