首页 > 解决方案 > 根据 numericInput 和 selectInput 创建表

问题描述

我正在尝试创建一个闪亮的应用程序,该应用程序根据兽医诊所的剂量和质量计算药物费率,然后技术人员可以打印生成的表格作为护理表。我已经看到 selectInput 可用于加载数据集并将其显示为表格的位置,但我需要根据动物的质量和药物浓度在表格中创建速率。例如,一种药物的剂量可能为 1、2、3、4 或 5 mcg/kg/hr,希望可以根据通过 selectInput 选择的药物加载,但我需要通过公式计算速率(在这种情况下(质量 * 剂量)/浓度)。由于每种药物具有不同的剂量,因此这也变得更加复杂,因此还需要根据 selectInput 进行反应。

到目前为止我所拥有的内容如下,但我无法生成任何响应来自闪亮应用程序的数据的表。

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)

我想获得“剂量”选项卡以生成剂量和速率表。很确定我可以为其他标签做类似的事情。有人可以指出我正确的方向吗?

标签: rshiny

解决方案


考虑到对我之前回答的评论,我添加了每种药物的一些数据,这些数据可能代表每种药物的不同剂量。

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)


  })

最终代码

用户界面

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 <- 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)

推荐阅读