Reputation: 41
I'm trying to make a web application using Rshiny which automatically calculates the titration rate of the infusion pump based on the drug dose and body weight. I have provided the formula for it in the attached google sheet file, but I'm having trouble making the web app version.
The "drug_dose" data that I used in the code, is in sheet 2 of this file
here is the shiny R code I tried to make, but it doesn't work.
ui <- fluidPage( tabPanel("Drugu dose",
# Give the page a title
titlePanel("Drug dose"),
helpText("calculates the titration rate of the infusion pump based on the drug dose and body weight"),
# left input
sidebarPanel(
numericInput(inputId = "bw", label = "body weight"),
checkboxGroupInput(inputId = "drug", label = "drug type", choices = c("Noradrenalin/vascon (4mg/50 cc)",
"NTG (50mg/50cc)"),
),
# right outputs
mainPanel(br(),
# h3("Results"),
br(),
tableOutput("table1"), width=6 )
)))
#
server <- shinyServer(function(input,output){
if(input$drug == "Noradrenalin/vascon (4mg/50 cc)")
{data <- drug_dose
data$dose <- data$dose_base * input$bw*60/80
df_subset <- reactive({
a <- subset(data, drug == input$drug)
return(a)
})
output$table1 <- renderTable(df_subset$dose_base, df_subset$dose)
}
else if (input$drug == "NTG (50mg/50cc)")
{data <- drug_dose
data$dose <- data$dose_base * input$bw*60/1000
df_subset <- reactive({
a <- subset(data, drug == input$drug)
return(a)
})
output$table1 <- renderTable(df_subset$dose_base, df_subset$dose)
}
})
shinyApp(ui = ui, server = server)
it says in the description that I need the active reactive context, but I'm not sure where and how to put the active reactive context.
I really appreciate your help, thank you
Upvotes: 0
Views: 54
Reputation: 125108
Besides some minor issues with your code, e.g. a numericInput
requires a value
the main issue is that you could use e.g. input$drug
only within a so-called reactive
. Moreover your code you be simplified, as there is no need for an if
statement:
library(shiny)
ui <- fluidPage(
tabPanel(
"Drugu dose",
# Give the page a title
titlePanel("Drug dose"),
helpText("calculates the titration rate of the infusion pump based on the drug dose and body weight"),
# left input
sidebarPanel(
numericInput(inputId = "bw", label = "body weight", value = 60),
checkboxGroupInput(inputId = "drug", label = "drug type",
choices = unique(drug_dose$drug)),
# right outputs
mainPanel(br(),
# h3("Results"),
br(),
tableOutput("table1"),
width = 6
)
)
)
)
server <- function(input, output) {
df_subset <- reactive({
data <- subset(drug_dose, drug %in% input$drug)
data$dose <- data$dose_base * input$bw * 60 / 80
data
})
output$table1 <- renderTable({
df_subset()
})
}
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:7317
Created on 2021-08-14 by the reprex package (v2.0.1)
DATA
drug_dose <- data.frame(
stringsAsFactors = FALSE,
dose_base = c(
0.1, 0.2, 0.3, 0.4, 0.5, 0.6,
0.7, 0.8, 0.9, 0.05, 0.5, 0.75, 1, 1.25, 1.5, 1.75, 2,
2.25, 2.5
),
drug = c(
"Noradrenalin/vascon (4mg/50 cc)", "Noradrenalin/vascon (4mg/50 cc)",
"Noradrenalin/vascon (4mg/50 cc)", "Noradrenalin/vascon (4mg/50 cc)",
"Noradrenalin/vascon (4mg/50 cc)",
"Noradrenalin/vascon (4mg/50 cc)", "Noradrenalin/vascon (4mg/50 cc)",
"Noradrenalin/vascon (4mg/50 cc)", "Noradrenalin/vascon (4mg/50 cc)",
"Noradrenalin/vascon (4mg/50 cc)", "NTG (50mg/50cc)",
"NTG (50mg/50cc)", "NTG (50mg/50cc)", "NTG (50mg/50cc)",
"NTG (50mg/50cc)", "NTG (50mg/50cc)", "NTG (50mg/50cc)",
"NTG (50mg/50cc)", "NTG (50mg/50cc)"
)
)
Upvotes: 2