根据numericInput和selectInput创建表

时间:2019-08-28 22:25:21

标签: r shiny

我正在尝试创建一个闪亮的应用程序,该应用程序可以根据兽医诊所的剂量和质量来计算药物使用率,然后技术人员可以将结果表打印为护理单。我已经看到了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)

我想获得“剂量”标签,以生成剂量和剂量表。可以肯定的是,我可以对其他选项卡执行类似的操作。有人可以指出我正确的方向吗?

1 个答案:

答案 0 :(得分:1)

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

因此,现在,选择药物后,将在下面的此方框中对该药物的所有预定义剂量进行计算

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

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)