0
votes

I am trying to create a shiny app that calculates drug rates based on dosages and mass for a veterinary clinic and then the tech can print the resulting table for a care sheet. I've seen where selectInput can be used to load a dataset and display it as a table, but I need to create the rates in the tables based upon the mass of the animal and drug concentration. For example, a drug may have a dosage of 1,2,3,4, or 5 mcg/kg/hr which can hopefully be loaded based upon the drug selected via selectInput, but I need the rate to be calculated by a formula (in this case (mass * dose)/concentration). This is further complicated by the fact that each drug has different dosages, so that also needs to be reactive based upon the selectInput.

What I have so far is below, but I haven't been able to get any tables to be produced that are responding to the data from the shiny app.

library(shiny)
drug.selections <- c("Fentanyl", "Ketamine", "Lidocaine", "Midazolam", "Metoclopramide", "Norepinephrine", "Dobutamine", 
                     "Dopamine", "Epinephrine", "Phenylephrine", "Propofol", "Dexmedetomidine 0.1 mg/dl", "Dexmedetomidine 0.5 mg/dl", "Diltiazem", "Furosemide", "Butorphanol",
                     "Hydromorphine", "Morphine", "Methadone", "ACA", "Magnesium", "Alfaxalone") 
drug.selections <- sort(drug.selections)
ui <- fluidPage(
  headerPanel("Continuous Rate Infusion"
  ),
  sidebarLayout(
    sidebarPanel(
      textInput("name", "Pet Name"),
      numericInput("mass", "Pet Weight (kg)", value = 0, min =0, max = 200),
      selectInput("drug", "Drug", drug.selections), 
      numericInput("concentration", "Concentration", value = 0, min = 0, max = 500),
      numericInput("time", "How many hours needed", value = 0, min = 0, max = 40),
      checkboxInput("dilute", "Dilute?", value = FALSE) 
    ),
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Dose", tableOutput("dose")),
                  tabPanel("Diluted Dose", tableOutput("ddose")),
                  tabPanel("Dilution Recipe", tableOutput("recipe")),
                  tabPanel("Summary", textOutput("summary")),
                  textOutput("name"), 
                  textOutput("mass"),
                  textOutput("drug")
      )
    )
  )
)

server <- function(input, output, session) {
  mass <- reactive({
    get(input$mass, inherits = FALSE)
  })


  output$summary <- renderText({
    paste (input$name, "weighs", input$mass, "kg and is on", input$concentration, "of", input$drug)
  })

  output$dose <- renderTable({
    dose
  })

  output$ddose <- renderTable({
    ddose
  })

  output$recipe <- renderTable({
    recipe
  })
}

shinyApp(ui, server)

I would like to get the "Dose" tab to produce a table of the dose and rate. Pretty sure I can do something similar for the other tabs. Can someone point me in the right direction?

1

1 Answers

1
votes

Taking into account the comment on my previous answer, I have added some data on each drug which might represent the different doses for each drug.

So now, when drug is selected, the calculations are performed on ALL the predefined dosages for that drug, in this block below

output$dose <- renderTable({

    dose = dosage_table[, input$drug]

    rate = dose *  as.numeric(input$mass) / as.numeric(input$concentration)

    data.frame(Dose = dose, Rate = rate)


  })

Final Code

UI

ui <- fluidPage(
  headerPanel("Continuous Rate Infusion"
  ),
  sidebarLayout(
    sidebarPanel(
      # add dosage
      textInput("name", "Pet Name"),
      numericInput("mass", "Pet Weight (kg)", value = 0, min =0, max = 200),
      selectInput("drug", "Drug", drug.selections), 
      numericInput("concentration", "Concentration", value = 0, min = 0, max = 500),
      numericInput("time", "How many hours needed", value = 0, min = 0, max = 40),
      checkboxInput("dilute", "Dilute?", value = FALSE) 
    ),
    mainPanel(
      tabsetPanel(type = "tabs",
                  tabPanel("Dose", tableOutput("dose")),
                  tabPanel("Diluted Dose", tableOutput("ddose")),
                  tabPanel("Dilution Recipe", tableOutput("recipe")),
                  tabPanel("Summary", textOutput("summary")),
                  textOutput("name"), 
                  textOutput("mass"),
                  textOutput("drug")
      )
    )
  )
)

Server

server <- function(input, output, session) {
  dosage_table <- data.frame('Fentanyl' = c(1,2,3,4,5,6,7,8,9,10), 
                             'Ketamine' = c(0.1,0.2,0.3,0.4,0.5),
                             "Lidocaine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Midazolam" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Metoclopramide" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Norepinephrine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Dobutamine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Dopamine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Epinephrine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Phenylephrine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Propofol" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Dexmedetomidine 0.1 mg/dl" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Dexmedetomidine 0.5 mg/dl" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Diltiazem" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Furosemide" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Butorphanol" = c(0.1,0.2,0.3,0.4,0.5),
                             "Hydromorphine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Morphine" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Methadone" = c(0.1,0.2,0.3,0.4,0.5), 
                             "ACA" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Magnesium" = c(0.1,0.2,0.3,0.4,0.5), 
                             "Alfaxalone" = c(0.1,0.2,0.3,0.4,0.5))

  mass <- reactive({
    get(input$mass, inherits = FALSE)
  })


  output$summary <- renderText({
    paste (input$name, "weighs", input$mass, "kg and is on", input$concentration, "of", input$drug)
  })

  output$dose <- renderTable({

    dose = dosage_table[, input$drug]

    rate = dose *  as.numeric(input$mass) / as.numeric(input$concentration)

    data.frame(Dose = dose, Rate = rate)


  })

  output$ddose <- renderTable({
    ddose
  })

  output$recipe <- renderTable({
    recipe
  })
}

# App
shinyApp(ui, server)