diff --git a/app/logic/constants.R b/app/logic/constants.R index 8fba395..9ff47ec 100644 --- a/app/logic/constants.R +++ b/app/logic/constants.R @@ -3,6 +3,12 @@ box::use(config) #' @export ORGAN_TAB <- config$get("tab_items", config = "organ_tab") +#' @export +GENDERS <- c("male", "female") + +#' @export +POPULATIONS <- c("European", "White American", "Black American", "Mexican American", "Asian", "Japanese") + #' @export COFFEE_TYPES <- list( `Espresso` = list(caffeine = 64, water = 30), diff --git a/app/logic/individual.R b/app/logic/individual.R index 81c6392..6087db0 100644 --- a/app/logic/individual.R +++ b/app/logic/individual.R @@ -4,6 +4,10 @@ box::use( dplyr[case_match] ) +box::use( + app / logic / constants[POPULATIONS, GENDERS], +) + set_individual <- function(simulation, user_data) { message("Applying individual characteristics to the simulation") # Create individual @@ -38,20 +42,21 @@ set_individual <- function(simulation, user_data) { } create_individual <- function(user_data) { + unit_system <- user_data$unit ind_carac <- ospsuite::createIndividualCharacteristics( species = "Human", population = translate_population(user_data$ethnicity), gender = translate_gender(user_data$gender), age = as.double(user_data$age), - weight = as.double(user_data$weight), - height = as.double(convert_height(user_data$height)), + weight = convert_weight(user_data$weight, unit_system), + height = convert_height(user_data$height, unit_system), seed = 42 ) return(ospsuite::createIndividual(ind_carac)) } translate_population <- function(population) { - rlang::arg_match(population, c("European", "White American", "Black American", "Mexican American", "Asian", "Japanese")) + rlang::arg_match(population, POPULATIONS) case_match( population, "European" ~ ospsuite::HumanPopulation$European_ICRP_2002, @@ -64,15 +69,30 @@ translate_population <- function(population) { } translate_gender <- function(gender) { - rlang::arg_match(gender, c("male", "female")) + rlang::arg_match(gender, GENDERS) case_match( gender, "male" ~ ospsuite::Gender$Male, "female" ~ ospsuite::Gender$Female ) } +convert_height <- function(height, unit_system) { + height <- if (unit_system == "imperial") { + # feet to cm + height * 30.48 + } else { + # meters to cm + height * 100 + } + return(as.double(height)) +} -convert_height <- function(height) { - # meters to cm - return(height * 100) +convert_weight <- function(weight, unit_system) { + weight <- if (unit_system == "imperial") { + # lbs to kg + weight * 0.453592 + } else { + weight + } + return(as.double(weight)) } diff --git a/app/logic/intakes.R b/app/logic/intakes.R index 71f084c..468966a 100644 --- a/app/logic/intakes.R +++ b/app/logic/intakes.R @@ -11,7 +11,7 @@ set_intakes <- function(simulation, intakes) { # Get all relevant paths time_paths <- purrr::map(ospsuite::getAllParametersMatching("**|Start time", simulation), "path") dose_paths <- purrr::map(ospsuite::getAllParametersMatching("**|Dose", simulation), "path") - volume_paths <- purrr::map(ospsuite::getAllParametersMatching("**|Volume of water/body weight", simulation), "path") + volume_paths <- purrr::map(ospsuite::getAllParametersMatching("Applications|Coffee Drinks|Solution|Application_*|ProtocolSchemaItem|Amount of water", simulation), "path") # Keep only enabled intakes enabled_intakes <- purrr::keep(intakes, ~ .x$selected) @@ -20,9 +20,8 @@ set_intakes <- function(simulation, intakes) { # Add the caffeine and water content to the intake # Set the intake in the simulation for (i in seq_along(enabled_intakes)) { - caffein_dose <- enabled_intakes[[i]]$type[[1]]$caffeine - water_volume <-enabled_intakes[[i]]$type[[1]]$water + water_volume <- enabled_intakes[[i]]$type[[1]]$water time <- enabled_intakes[[i]]$time # Set the caffeine dose @@ -42,11 +41,16 @@ set_intakes <- function(simulation, intakes) { ) # Set the volume of water - # TODO + ospsuite::setParameterValuesByPath( + parameterPaths = volume_paths[[i]], + values = water_volume, + unit = "ml", + simulation = simulation + ) } # Set remaining intakes to 0 - for (j in (i+1):length(dose_paths)) { + for (j in (i + 1):length(dose_paths)) { if (j > length(dose_paths)) break ospsuite::setParameterValuesByPath( @@ -66,14 +70,14 @@ set_intakes <- function(simulation, intakes) { ospsuite::setParameterValuesByPath( parameterPaths = volume_paths[[j]], values = 0, - unit = "l/kg", + unit = "ml", simulation = simulation ) } } -convert_time_to_min <- function(time){ +convert_time_to_min <- function(time) { # Convert time to minutes from midnight time <- as.POSIXct(time, format = "%H:%M") diff --git a/app/logic/plot.R b/app/logic/plot.R index 484d46d..51496fa 100644 --- a/app/logic/plot.R +++ b/app/logic/plot.R @@ -3,7 +3,7 @@ box::use( ospsuite ) -get_plot <- function(simulation_results){ +get_plot <- function(simulation_results) { message("Generating time profile plot") dc <- ospsuite::DataCombined$new() @@ -14,8 +14,5 @@ get_plot <- function(simulation_results){ dc$addSimulationResults(simulation_results[[1]]) - return(ospsuite::plotIndividualTimeProfile(dc,defaultPlotConfiguration = pc)) - + return(ospsuite::plotIndividualTimeProfile(dc, defaultPlotConfiguration = pc)) } - - diff --git a/app/logic/simulation.R b/app/logic/simulation.R index 5ee3a45..3439b43 100644 --- a/app/logic/simulation.R +++ b/app/logic/simulation.R @@ -4,9 +4,17 @@ box::use( ) -load_simulation <- function(){ +load_simulation <- function() { message("Initializing Simulation") simulation <- ospsuite::loadSimulation("app/logic/coffee-sim.pkml") - ospsuite::setOutputInterval(simulation, 0, 24*60-1, resolution = 1/2) + ospsuite::setOutputInterval(simulation, 0, 24 * 60 - 1, resolution = 1 / 2) + ospsuite::setOutputs( + c( + "Organism|PeripheralVenousBlood|Caffeine|Plasma (Peripheral Venous Blood)", + "Organism|Brain|Intracellular|Caffeine|Concentration", + "Organism|Heart|Intracellular|Caffeine|Concentration" + ), + simulation + ) return(simulation) } diff --git a/app/main.R b/app/main.R index a62981b..e93a1b7 100644 --- a/app/main.R +++ b/app/main.R @@ -61,6 +61,5 @@ server <- function(id) { app_data$destroy_intro_screen <- TRUE result_screen$server("result_screen", app_data) }) - }) } diff --git a/app/view/intro_screen.R b/app/view/intro_screen.R index d36c83e..d0a26bd 100644 --- a/app/view/intro_screen.R +++ b/app/view/intro_screen.R @@ -3,7 +3,7 @@ box::use( shiny[moduleServer, NS, tagList, observeEvent], ) box::use( - app/logic/constants[COFFEE_TYPES], + app/logic/constants[COFFEE_TYPES, POPULATIONS], app/view/react[intro_screen_page], # Import the component. ) @@ -14,10 +14,10 @@ ui <- makeModuleUIDestroyable( tagList( intro_screen_page( id = ns("stepper"), - ethinicity_options = c("European", "White American", "Black American","Mexican American", "Asian", "Japanese"), + ethinicity_options = POPULATIONS, metabolism_options = c("low", "normal", "high"), coffee_type_options = COFFEE_TYPES, - unit="metric", + unit = "metric", unit_options = c("metric", "imperial"), init_shiny_data = list( ethnicity = "European", @@ -60,21 +60,20 @@ server <- makeModuleServerDestroyable( # Observe User Data changes observeEvent(input$stepper_userdata, { app_data$user_data <- input$stepper_userdata - print(app_data$user_data) #! dev + print(app_data$user_data) # ! dev }) # Observe Intake Data changes observeEvent(input$stepper_intake, { app_data$intake_data <- input$stepper_intake$intakes app_data$destroy_intro_screen <- TRUE - print(app_data$intake_data) #! dev + print(app_data$intake_data) # ! dev }) # Remove loader when calcularion finished observeEvent(app_data$destroy_intro_screen, { destroyModule() }) - }) } ) diff --git a/app/view/result_screen.R b/app/view/result_screen.R index 4a9bacc..5fd924c 100644 --- a/app/view/result_screen.R +++ b/app/view/result_screen.R @@ -12,8 +12,8 @@ box::use( ui <- function(id) { ns <- NS(id) tagList( - uiOutput(ns("result_screen")), - fluidRow(column(plotOutput(ns("plot")), width = 10, offset = 1)) + uiOutput(ns("result_screen")) + ) } @@ -34,7 +34,8 @@ server <- function(id, app_data) { div( style = "margin-top: 2rem;", organ_info_tabpanel$ui(session$ns("organ_info_tabpanel")) - ) + ), + fluidRow(column(plotOutput(session$ns("plot")), width = 10, offset = 1)) ) })