Combination of oral and iv administrations















[LONGITUDINAL]
input = {F, ka, V, Cl}

PK:
depot(type=1, target=Ad, p=F)
depot(type=2, target=Ac)

EQUATION:
k = Cl/V
ddt_Ad = -ka*Ad
ddt_Ac =  ka*Ad - k*Ac
Cc = Ac/V

        
shinyUI(fluidPage(
   titlePanel(
     list(HTML('<p style="color:#4C0B5F; font-size:24px" fontsize=14>Combination of oral and iv administrations</p>' )),
     windowTitle="oral and iv"),
    tabsetPanel(
    tabPanel("Plot",
        fluidRow(
          column(3,
            tabsetPanel(
              tabPanel("oral",
                       sliderInput("tfd1", "time of first dose:", value=0, min=0, max = 20, step=1),
                       sliderInput("nd1", "number of doses:", value=1, min=0, max = 20, step=1),
                       sliderInput("ii1", "interdose interval:", value = 6, min = 0.5, max = 15, step=0.5),
                       sliderInput("amt1", "amount:", value = 5, min = 0, max = 20, step=1),
                       br(),                              
                       br()                               
              ),
              tabPanel("iv",
                       sliderInput("tfd2", "time of first dose:", value=0, min=0, max = 20, step=1),
                       sliderInput("nd2", "number of doses:", value=0, min=0, max = 10, step=1),
                       sliderInput("ii2", "interdose interval:", value = 9, min = 0.5, max = 15, step=0.5),
                       sliderInput("tinf2", "infusion time:", value = 1, min = 0, max = 10, step=0.5),
                       sliderInput("amt2", "amount:", value = 5, min = 0, max = 20, step=1),
                       br(),                              
                       br()                               
              ),
              tabPanel("parameters",
                       sliderInput("F", "bioavailability F:", value = 0.7, min = 0, max = 1, step=0.05),
                       br(),
                       sliderInput("ka", "absorption rate constant ka:", value = 0.5, min = 0, max = 4, step=0.1), 
                       br(),
                       sliderInput("V", "volume V:", value = 10, min = 1, max = 20, step=1),
                       br(),
                       sliderInput("Cl", "clearance Cl:", value = 1, min = 0, max = 4, step=0.1),
                       br(),                              
                       br()                               
              ),
              tabPanel("outputs",
                       selectInput("output","",c("concentration (central)" = "Cc",
                                                 "amount (central)" = "Ac", "amount (depot)" = "Ad")),
                       br(),
                       sliderInput("range", "time range", min = -10, max = 200, value = c(-5,100), step=5),
                       br(),
                       sliderInput("ngp", "grid size", min = 10, max = 1000, value = 200, step=10),
                       br()                               
              ),
              tabPanel("settings",
                       sliderInput("lsize", "line size", min = 0, max = 3, value = 0.75, step=0.25),
                       br(),
                       checkboxInput("log", "Plot y axis on log scale", value = FALSE),
                       br()                               
              )
            )),
       column(9,
          plotOutput("plot",  height="500px"))
       )),
    tabPanel("Table", tableOutput("table")),
    tabPanel("Mlxtran", pre(includeText("pk2_model.txt"))),
    tabPanel("Simulx", verbatimTextOutput("RFile")),
    tabPanel("ui.R", pre(includeText("ui.R"))),
    tabPanel("server.R", pre(includeText("server.R")))
  )
))
my.dir=getwd()
source("../../initMlxR.R")
setwd(my.dir)

simulx.text <- ("
              parameter = p,
              output    = Cc)

print(ggplot(data=res$Cc, aes(x=time, y=Cc)) + geom_line() +
        xlab('time (h)') + ylab('concentration (mg/L)'))
")

shinyServer(function(input, output) {
  
  r <- reactive({  
    if (input$tinf2>input$ii2)
      stop("The infusion time should not exceed the interdose interval")
    
    param.value=c(input$F,input$ka,input$V,input$Cl)
    t.value=seq(input$range[1],input$range[2],length.out=input$ngp)
    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, rate=input$amt2/input$tinf2, type=2)
    }else{
      adm2 <- list(time=t21, amount=0, type=2)
    }
    
    f  <- list(name=c('Ac','Ad','Cc'),time=t.value)
    p   <- list(name=c('F','ka','V','Cl'), value=param.value)
    
    res <- simulx( model     = 'pk2_model.txt',
                   treatment = list(adm1,adm2), 
                   parameter = p,
                   output    = f)
    
    if (input$nd1==0){
      a1.text <- ''
    } else if(input$nd1==1){
      a1.text <- paste0('adm1 <- list(type=1, amount=',input$amt1,', time=',input$tfd1,')\n')
    } else{
      t1=input$tfd1
      t2=input$ii1*(input$nd1-1)+t1
      a1.text <- paste0('adm1 <- list(type=1, amount=',input$amt1,
                        ', time=seq(',t1,', ',t2,', by=',input$ii1,'))\n')
    }
    if (input$nd2==0){
      a2.text <- ''
    }else{
      if(input$nd2==1){
        a2.text <- paste0('adm2 <- list(type=2, amount=',input$amt2,', time=',input$tfd2)
      } else{
        t1=input$tfd2
        t2=input$ii2*(input$nd2-1)+t1
        a2.text <- paste0('adm2 <- list(type=2, amount=',input$amt2,
                          ', time=seq(',t1,', ',t2,', by=',input$ii2,')')
      }
      if (input$tinf2>0){
        a2.text <- paste0(a2.text,', tinf=',input$tinf2,')\n')
      }else{
        a2.text <- paste0(a2.text,')\n')      
      }
    }
    a.text <- paste0(a1.text,a2.text)
    
    m.text <- "res <- simulx(model     = 'pk2_model.txt',\n              "
    if (input$nd1>0){
      if (input$nd2>0){
        m.text <- paste0(m.text,'treatment = list(adm1, adm2),')
      }else{
        m.text <- paste0(m.text,'treatment = adm1,')        
      }
    }else{
      if (input$nd2>0){
        m.text <- paste0(m.text,'treatment = adm2,')        
      }      
    }
    
    p.text <- paste0('p <- c(F=',input$F,', ka=',input$ka,', V=',input$V,', Cl=',input$Cl,')')
    t.text <- paste0('t <- seq(',input$range[1],', ',input$range[2],
                     ', length=',length.out=input$ngp,')')
    simulx.text <- paste0(a.text,p.text,'\n',t.text,'\n',m.text,simulx.text)
    
    out <- list(res,simulx.text)
    return(out)
  })
  
  output$plot <- renderPlot({
    r=r()[[1]]
    r <- r[[input$output]]
    j<-which(names(r)==input$output)
    names(r)[j] <- "f"
    pl <- ggplot(data=r, aes(x=time, y=f)) + geom_line(size=input$lsize) +
      ylab(input$output)
    if (input$log==TRUE)
      pl <- pl + scale_y_log10()
    
    print(pl)
  })
  
  output$table <- renderTable({r()[[1]]$Cc})
  output$RFile  <- renderText(r()[[2]])
  
  
})