DESCRIPTION:
PKPD model. The PK data must be tagged with the lowest OBSERVATION ID value. 
The administration is extravascular with a first order absorption (rate constant ka).
The PK model has one compartment (volume V) and a linear elimination (clearance Cl).

[LONGITUDINAL]
input = {ka, V, Cl, BS, kout, R0, Emax, gamma, EC50, Kendo,IC1, Ga, ANT}
ANT= {use= regressor}


EQUATION:
;====== PK part of the model

f1 = pkmodel(ka,V,Cl)
k=Cl/V

;==Antagonist
AE= Ga/(Ga+1)
if  (ANT==1)
Kendo1= Kendo*(1- AE)
else
 Kendo1= Kendo
end

;====== PD part of the model
;initial values
R_0 = R0
f2_0= BS
;parameter transformation
kin =R0*kout  
ddt_f2= Kendo1*(1 - max(R,0)/(max(R,0)+IC1)) - k*f2
Cc= f1 + f2
ddt_R = kin * (1 + Emax * max(Cc,0)^gamma/(max(Cc,0)^gamma+EC50^gamma)) - kout * R 
ddt_auc= R

OUTPUT: 
output = {Cc,R}
table= {auc}
ui <- shinyUI(fluidPage(
navbarPage("Predicted Inhibin B Levels (pg/ml)",
tabPanel("Plot",
fluidRow(
column(3,br(),
tabsetPanel(type='tabs',
tabPanel('param1',br(),
sliderInput("ka", label="ka",value=0.14,min=0.07,max=0.28,step=0.014),
sliderInput("BS", label="BS",value=0.5,min=0.25,max=1,step=0.05),
sliderInput("Cl", label="Cl",value=0.88,min=0.44,max=1.76,step=0.088),
sliderInput("V", label="V",value=21.8,min=10.9,max=43.6,step=2.18),
br()
),
tabPanel('param2',br(),
sliderInput("Kendo", label="Kendo",value=0.024,min=0.012,max=0.048,step=0.0024),
sliderInput("Ga", label="Ga",value=0.27,min=0.135,max=0.54,step=0.027),
br()
),
tabPanel('param3',br(),
sliderInput("EC50", label="EC50",value=1.2,min=0.6,max=2.4,step=0.12),
sliderInput("Emax", label="Emax",value=86.9,min=43.45,max=173.8,step=8.69),
sliderInput("R0", label="R0",value=41,min=20.5,max=82,step=4.1),
sliderInput("kout", label="kout",value=1.13,min=0.565,max=2.26,step=0.113),
sliderInput("IC1", label="IC1",value=333,min=166.5,max=666,step=33.3),
sliderInput("gamma", label="gamma",value=4.31,min=2.155,max=8.62,step=0.431),
br()
),
tabPanel('adm',br(),
sliderInput("amount1", label="amount",value=16.5,min=8.25,max=33,step=1.65),
br()
),
tabPanel('output',br(),

fluidRow(
column(5,checkboxInput("boxref", label="reference")),
column(4,actionButton("butref", label = "Reset"))
),
hr(),
radioButtons("ilog", "scale", c("linear" = FALSE,"log" = TRUE), inline=TRUE),
br()
),
br()
)

),
column(9,br(),            
plotOutput("plot")
)
)
),
tabPanel("Mlxtran", pre(includeText("model.txt"))),
tabPanel("ui.R", pre(includeText("ui.R"))),
tabPanel("server.R", pre(includeText("server.R")))
)
))
library("mlxR")
library("gridExtra")
source("shinymlxTools.R")
load("data.RData")

f <- list(name='R', time=seq(0,288,by=24))
f <- list(f)
nf <- length(f)
info <- info_res(f)

server <- function(input, output) {
  ref <- reactive({
    input$butref
    p <- list(name  = c('ka', 'BS', 'Cl', 'V', 'Kendo', 'Ga', 'EC50', 'Emax', 'R0', 'kout', 'IC1', 'gamma'),
              value = isolate(c(input$ka, input$BS, input$Cl, input$V, input$Kendo, input$Ga, input$EC50, input$Emax, input$R0, input$kout, input$IC1, input$gamma)))
    t.dose <- c(0,24,48,72,96,120,144,168,192,216,240,264,288)
    adm <- list(time=t.dose, amount=isolate(as.numeric(input$amount1)))
    r <- simulx( model     = 'model.txt',
                 treatment = adm,
                 parameter = p,
                 regressor = regressor,
                 group     = group,
                 output    = f)

    ref <- merge_res(r,f)
    return(ref)
  })
  
  res <- reactive({
    p <- list(name  = c('ka', 'BS', 'Cl', 'V', 'Kendo', 'Ga', 'EC50', 'Emax', 'R0', 'kout', 'IC1', 'gamma'),
              value = c(input$ka, input$BS, input$Cl, input$V, input$Kendo, input$Ga, input$EC50, input$Emax, input$R0, input$kout, input$IC1, input$gamma))
    t.dose <- c(0,24,48,72,96,120,144,168,192,216,240,264,288)
    adm <- list(time=t.dose, amount=as.numeric(input$amount1))                     
    r <- simulx( model     = 'model.txt',
                 treatment = adm,
                 parameter = p,
                 regressor = regressor,
                 group     = group,
                 output    = f)
                     
    res <- merge_res(r,f)
    return(res)
  })  
  
  output$plot <- renderPlot({
    res=res()
   ref=ref()    
    gr.txt <- "grid.arrange("
    for (j in (1:length(f))){
      xj <- "time"
      fj <- f[[j]]
      name.fj <- fj$name

   eval(parse(text=paste0("inputyj=input$out",j)))  
  i.plot=FALSE
  if (!is.null(inputyj)){
    ij <- which(name.fj %in% inputyj)
    if (length(ij>0)){
      eval(parse(text=paste0("inputxj=input$x",j)))
      if (!is.null(inputxj))
        xj <- inputxj
    }
    i.plot=TRUE
  }
  else if (is.null(inputyj) & length(f)==1){
    ij=1
    i.plot=TRUE
  }
  if (i.plot){
    pl <- ggplotmlx()
    nfj <- length(name.fj)
    for (k in (1:nfj)){
      if (k %in% ij){
           if (input$boxref==TRUE){
      pj <- paste0('pl <- pl + geom_path(data=ref[[j]], aes(x=time,y=',name.fj[k],'),colour="grey",size=0.75)')
      eval(parse(text=pj))
    }
       pj <- paste0('pl <- pl + geom_path(data=res[[j]], aes(x=time,y=',name.fj[k],',colour="',info[[j]]$colour[k],'"),size=0.75)')
       eval(parse(text=pj))
      }  
    }
    pl <- pl + scale_colour_manual(values=info[[j]]$values, labels=info[[j]]$labels)
    print(pl)
    if (length(ij)>1){
      if (!is.null(input$legend) && input$legend==FALSE)
        pl <- pl + theme(legend.position="none")
      else
        pl <- pl + guides(colour=guide_legend(title=NULL)) + theme(legend.position=c(.9, .8))
      pl <- pl + ylab("")
    }else{
      pl <- pl + theme(legend.position="none")
    }
  
      if (input$ilog==TRUE)
       pl=pl + scale_y_log10()
    eval(parse(text=paste0("pl",j," <- pl")))
    gr.txt <- paste0(gr.txt,"pl",j,",")
  }
    }
    gr.txt <- paste0(gr.txt,"ncol=1)")
    eval(parse(text=gr.txt))
  }, height = 500)
}