DESCRIPTION:
One compartment TMDD model assuming doses injected directly into central compartment

[LONGITUDINAL]
input = {kel, kep, kon, kout, koff, Vc, R0}

PK:
; Dosemg: Dose injected into central compartment (mg/kg)
; MWlig=150000; molecular weight of ligand (Da) 
; (Dosemg*1e-3/MWlig)*1e9 ; Dose conversion (nmol/kg)

depot(target=L, p=100/15/Vc)  ; L is a concentration (nM)

EQUATION:
kin   = kout*R0 ; Synthesis of receptor (nM/day)

;Initial Conditions
L_0 = 0
R_0 = R0
P_0 = 0

;Ordinary Differential Equations
ddt_L = -kel*L-kon*L*R+koff*P 
ddt_R = kin-kout*R-kon*L*R+koff*P
ddt_P = kon*L*R-koff*P-kep*P

;Ltot and Rtot
Lt = L+P; Total concentration of ligand (nM)
Rt = R+P; Total concentration of receptor (nM)
ui <- shinyUI(fluidPage(
  navbarPage("TMDD - Model 1",
             tabPanel("Plot",
                      fluidRow(
                        column(2,
                               tabsetPanel(type='tabs',
                                           tabPanel('param1',br(),
                                                    sliderInput("kel", label="kel",value=0.024,min=0.012,max=0.048,step=0.0024),
                                                    sliderInput("kep", label="kep",value=0.201,min=0.1005,max=0.402,step=0.0201),
                                                    sliderInput("kout", label="kout",value=0.823,min=0.4115,max=1.646,step=0.0823),
                                                    sliderInput("koff", label="koff",value=0.9,min=0.45,max=1.8,step=0.09),
                                                    br()
                                           ),
                                           tabPanel('param2',br(),
                                                    sliderInput("kon", label="kon",value=0.592,min=0.296,max=1.184,step=0.0592),
                                                    sliderInput("R0", label="R0",value=2.688,min=1.344,max=5.376,step=0.2688),
                                                    sliderInput("Vc", label="Vc",value=0.04,min=0.02,max=0.08,step=0.004),
                                                    br()
                                           ),
                                           tabPanel('dose',br(),
                                                    sliderInput("tfd1", label="tfd",value=12,min=0,max=24,step=2),
                                                    sliderInput("nd1", label="nd",value=1,min=1,max=10,step=1),
                                                    sliderInput("ii1", label="ii",value=12,min=4,max=24,step=2),
                                                    sliderInput("amount1", label="amount",value=1,min=0,max=4,step=0.2),
                                                    br()
                                           ),
                                           br()
                               )
                               
                        ),
                        column(2,
                               br(),
                               checkboxGroupInput("out1", label="plot 1", choices='L', selected='L'),
                               
                               selectInput("x1", label="v.s.",c('time','L','R','P')),
                               hr(),
                               checkboxGroupInput("out2", label="plot 2", choices='R', selected='R'),
                               
                               selectInput("x2", label="v.s.",c('time','L','R','P')),
                               hr(),
                               checkboxGroupInput("out3", label="plot 3", choices='P', selected='P'),
                               
                               selectInput("x3", label="v.s.",c('time','L','R','P')),
                               hr()
                        ),
                        column(8,br(),            
                               plotOutput("plot"),
                               br(),
                               br(),
                               br(),
                               br(),
                               br(),
                               fluidRow(
                                 column(1),
                                 column(1,checkboxInput("boxref", label="ref.")),
                                 column(3,actionButton("butref", label = "Reset")),
                                 column(7,radioButtons("ilog", "scale", c("linear" = FALSE,"log  " = TRUE), inline=TRUE))
                               )
                        )
                      )
             ),
             tabPanel("Model", pre(includeText("model.txt"))),
             tabPanel("ui.R", pre(includeText("ui.R"))),
             tabPanel("server.R", pre(includeText("server.R")))
  )
))
library("mlxR")
library("reshape")
library("gridExtra")
source("shinymlxTools.R")

out1 <- list(name='L', time=seq(0,100,by=1))
out2 <- list(name='R', time=seq(0,100,by=1))
out3 <- list(name='P', time=seq(0,100,by=1))
f  <- list(out1, out2, out3)
nf <- length(f)
info <- info_res(f)

server <- function(input, output) {
  ref <- reactive({
    input$butref
    p <- list(name  = c('kel', 'kep', 'kout', 'koff', 'kon', 'Vc', 'R0'),
              value = isolate(c(input$kel, input$kep, input$kout, input$koff, input$kon, input$Vc, input$R0)))
    t1 <- isolate(input$tfd1)
    t2 <- isolate(input$ii1)*(isolate(input$nd1)-1)+t1
    t.dose <- seq(t1,t2,by=isolate(input$ii1))
    adm <- list(time=t.dose, amount=isolate(input$amount1))
    r <- simulx( model     = 'model.txt',
                 treatment = adm,
                 parameter = p,
                 output    = f)

    ref <- merge_res(r,f)
    return(ref)
  })
  
  res <- reactive({
    p <- list(name  = c('kel', 'kep', 'kout', 'koff', 'kon', 'Vc', 'R0'),
              value = c(input$kel, input$kep, input$kout, input$koff, input$kon, input$Vc, input$R0))
    t1 <- input$tfd1
    t2 <- input$ii1*(input$nd1-1)+t1
    t.dose <- seq(t1,t2,by=input$ii1)
    adm <- list(time=t.dose, amount=input$amount1)                     
    r <- simulx( model     = 'model.txt',
                 treatment = adm,
                 parameter = p,
                 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, aes(x=',xj,',y=',name.fj[k],'),colour="grey",size=0.75)')
      eval(parse(text=pj))
    }
   pj <- paste0('pl <- pl + geom_path(data=res, aes(x=',xj,',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)
}